Javascript進行HTML轉義

  1. $package("js.lang");// 沒有包管理時,也可簡單寫成 js = {lang:{}};

  2.  

  3. js.lang.String=function(){

  4.  

  5.    this.REGX_HTML_ENCODE =/"|&|'|<|>|[\x00-\x20]|[\x7F-\xFF]|[\u0100-\u2700]/g;

  6.  

  7.    this.REGX_HTML_DECODE = /&\w+;|&#(\d+);/g;

  8.  

  9.    this.REGX_TRIM = /(^\s*)|(\s*$)/g;

  10.  

  11.    this.HTML_DECODE = {

  12.        "&lt;"  : "<", 

  13.        "&gt;"  : ">", 

  14.        "&amp;" : "&", 

  15.        "&nbsp;": "", 

  16.        "&quot;": "\"",

  17.        "&copy;":""

  18.  

  19.        // Add more

  20.    };

  21.  

  22.    this.encodeHtml =function(s){

  23.        s =(s !=undefined)? s :this.toString();

  24.        return(typeof s !="string")? s :

  25.            s.replace(this.REGX_HTML_ENCODE,

  26.                      function($0){

  27.                          var c = $0.charCodeAt(0), r =["&#"];

  28.                          c =(c == 0x20)? 0xA0 : c;

  29.                          r.push(c); r.push(";");

  30.                          return r.join("");

  31.                      });

  32.    };

  33.  

  34.    this.decodeHtml =function(s){

  35.        var HTML_DECODE =this.HTML_DECODE;

  36.  

  37.        s =(s !=undefined)? s :this.toString();

  38.        return(typeof s !="string")? s :

  39.            s.replace(this.REGX_HTML_DECODE,

  40.                      function($0, $1){

  41.                          var c = HTML_DECODE[$0];

  42.                          if(c ==undefined){

  43.                              // Maybe is Entity Number

  44.                              if(!isNaN($1)){

  45.                                  c =String.fromCharCode(($1 == 160) ? 32:$1);

  46.                              }else{

  47.                                  c = $0;

  48.                              }

  49.                          }

  50.                          return c;

  51.                      });

  52.    };

  53.  

  54.    this.trim =function(s){

  55.        s =(s !=undefined)? s :this.toString();

  56.        return(typeof s !="string")? s :

  57.            s.replace(this.REGX_TRIM,"");

  58.    };

  59.  

  60.  

  61.    this.hashCode =function(){

  62.        varhash=this.__hash__, _char;

  63.        if(hash==undefined||hash== 0){

  64.            hash= 0;

  65.            for(var i = 0, len=this.length; i < len; i++){

  66.                _char =this.charCodeAt(i);

  67.                hash= 31*hash+ _char;

  68.                hash=hash&hash;// Convert to 32bit integer

  69.            }

  70.            hash=hash& 0x7fffffff;

  71.        }

  72.        this.__hash__ =hash;

  73.  

  74.        returnthis.__hash__;

  75.    };

  76.  

  77. };

  78.  

  79. js.lang.String.call(js.lang.String);

  在實際的使用中可以有兩種方式:

 

 1)使用js.lang.String.encodeHtml(s)和js.lang.String.decodeHtml(s)。

 

 2)還可以直接擴展String的prototype

  

  1. js.lang.String.call(String.prototype);

  2.  

  3.  // 那麼

  4.  

  5.  var str ="<B>&'\"中國</B>abc   def";

  6.  

  7.  var ec_str = str.encodeHtml();

  8.  

  9.  document.write(ec_str);

  10.  

  11.  document.write("<bt><bt>"); 

  12.  

  13.  var dc_str = ec_str.decodeHtml();

  14.  

  15.  document.write(dc_str);

發佈了52 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章