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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值