日期:2014-05-16 浏览次数:20364 次
<script type="text/javascript"> var App = { chars : "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", rplstr : "~!@#$%^&*", //要过滤的字符 encode : function(str){ var self = this; var reg = new RegExp("\\" + this.rplstr.split('').join('|\\'), "g"); str = str.replace(reg, function(v){return "{"+ (self.chars.indexOf(v) + 32) + "}";}); return str; }, decode : function(str){ var self = this; str = str.replace(/{\d+}/g, function(v){return self.chars.charAt(parseInt(v.replace(/{|}/g, ""), 10)-32);}); return str; } } var str = "你好@{123*&%ABC" str = App.encode(str); alert(str); str = App.decode(str); alert(str); </script>
------解决方案--------------------
var p = /(@|%40)/; var t = "你好@"; t = t.replace(p,"-"); alert(t); var p0 = /(-|%2D)/; t = t.replace(p0,"@"); alert(t);