replaceAll在谷歌中可以使用,在ie和360不可以,会报错:
解决办法:使用replace去替换replaceAll
- 字符替换
string.replace(/s/g, ‘b’);这里需要注意的是全局替换星号时需要string.replace(/\*/g, ‘b’) - 变量替换
string.replace(new RegExp(str, ‘gm’), “b”) - 给 string 对象添加 replaceAll() 方法
String.prototype.replaceAll = function(s1, s2){
// g:执行全局匹配,而不是匹配第一个后停止。
// m:执行多行匹配
return this.replace(new RegExp(s1,“gm”),s2);
}; - 使用 split 和 join
a = a.split(old).join(new);