JS:app
String.prototype.replaceAll=function(reallyDo, replaceWith) {
return this.replace(new RegExp(reallyDo, "g" ), replaceWith);
}this
使用方式:spa
var str="addfgfdgfdgfdbvcmbadd,vm";prototype
alert(str.replaceAll("add","app"));it
弹出结果:appfgfdgfdgfdbvcmbapp,vmio
---------------------------------------------------------------function
网上有直接所有替换的方法:变量
var str='你们都是男人吗,难道不是吗?';
str = str.replace(/吗/g, '嘛');
alert(str);配置
表达式 /吗/g 这里的g,这个g表示全局,不给这个配置项是不会所有替换的。方法
只是这个“吗”,不能替换为变量,因此用正则参数带入返回。