由于javascript中的replace函数无法替换全部匹配的字符串,所以需要为String类增加一个方法,代码如下:
复制代码 代码如下:
String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {
if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);
} else {
return this.replace(reallyDo, replaceWith);
}
}
最后
以上就是羞涩马里奥最近收集整理的关于JavaScript实现的字符串replaceAll函数代码分享的全部内容,更多相关JavaScript实现内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复