实现JS中的 replaceAll 方法

来源:互联网 发布:如何入驻天猫淘宝商城 编辑:程序博客网 时间:2024/06/10 01:41
由于JS中没有replaceAll方法
于是我们可以这样改造一下,添加String对象的原型方法
String.prototype.replaceAll = function replaceAll(str1,str2){
     return this.replace(new RegExp(str1,'gm'),str2);
}


使用

var testStr = '1,2,3,4,5,6';

testStr.replaceAll(',','-');
1 0
原创粉丝点击