js 字符串全部替换

JS:

String.prototype.replaceAll=function(reallyDo, replaceWith) {
    return this.replace(new RegExp(reallyDo, "g" ), replaceWith);
}

 

使用方式:

var str="addfgfdgfdgfdbvcmbadd,vm";

alert(str.replaceAll("add","app"));

 

弹出结果:appfgfdgfdgfdbvcmbapp,vm

 

---------------------------------------------------------------

 

网上有直接全部替换的方法:

var str='大家都是男人吗,难道不是吗?';
str = str.replace(/吗/g, '嘛');
alert(str);

表达式 /吗/g 这里的g,这个g表示全局,不给这个配置项是不会全部替换的。

只是这个“吗”,不能替换为变量,所以用正则参数带入返回。

 

posted @ 2014-08-21 14:54  呓语  阅读(1046)  评论(0编辑  收藏  举报
welcome to this garden! --Chenly