js的replaceAll()

js中没有java中的replaceAll()函数,为了达到与java的replaceAll()一样的效果,我们可以用如下代码实现:

String.prototype.replaceAll = function(s1,s2) {

    return this.replace(new RegExp(s1,"gm"),s2);

}

调用方式:

如想替换字符串"aa bb cc"中的所有空格,可以执行"aa bb cc".replaceAll(" ", "");

$("<b>Paragraph. </b>").replaceAll("p");

原文地址:https://www.cnblogs.com/timy/p/1804908.html