天天看点

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");