JavaScript中只有replace方法,并不提供replaceAll方法,再此只能自己写replaceAll方法了。
String.prototype.replaceAll = function(s1,s2){
return this.replace(new RegExp(s1,"gm"),s2);
}
本文介绍了在JavaScript中实现字符串的replaceAll方法,因为标准库中只提供了replace方法,所以需要手动编写一个代替方法来实现全字符串替换的功能。
JavaScript中只有replace方法,并不提供replaceAll方法,再此只能自己写replaceAll方法了。
String.prototype.replaceAll = function(s1,s2){
return this.replace(new RegExp(s1,"gm"),s2);
}
702

被折叠的 条评论
为什么被折叠?