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表示全局,不給這個配置項是不會所有替換的。方法
只是這個「嗎」,不能替換爲變量,因此用正則參數帶入返回。