escape : function(string) {
return string.replace(/('|\\)/g, "\\$1");
}
var s=String.escape("天生一个'仙人\洞,无限风光'在险\峰.");
alert(s);
下面是结果这儿的首次匹配是\替换掉在string中出现的‘或\   
子匹配$1中还是一样,为什么  天生一个'仙人\洞,无限风光'在险\峰.中的\最终没有了???functionRegExp

解决方案 »

  1.   

    你的字符串"天生一个'仙人\洞,无限风光'在险\峰."中其实是没有'\'的  他是对洞和峰进行转义了  依然为洞和峰你改成这样试试
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript">
    String.prototype.change=function(){
    var reg=/('|\\)/g;
    return this.replace(reg,'\\$1');
    }
    function ss(a){
    a=a.change();
    alert(a);
    }
    </script>
    </head><body>
    <input type="text" value="天生一个'仙人\洞,无限风光'在险\峰." onblur="ss(this.value)">
    </body>
    </html>
      

  2.   

    试试这个吧兄弟,alert("\洞");  你就会发现你在鸡蛋中找骨头