美文网首页
replaceAll 兼容性问题

replaceAll 兼容性问题

作者: 香蕉多加君 | 来源:发表于2022-07-11 11:20 被阅读0次

低版本浏览器普遍不支持replaceAll, 所以谨慎使用此方法。

解决replaceAll 不兼容问题方法:

1: 用replace替代

       string.replace(/oldString/g, newString)     replace 默认只会匹配替换到匹配到的第一个,因此要加  / /g

2 : 第二种也是用replace方法。  

                string.replace( new  RegExp( "oldString", "gm" ),  "newString" )

3 给String对象扩展原型方法  replaceAll

String.prototype.replaceAll = function(newString, oldString) {

        return this.replace( new  RegExp( "oldString", "gm" ),  "newString" )

}

参考文章: https://wenku.baidu.com/view/4734c2f2d25abe23482fb4daa58da0116c171f8f.html

相关文章

网友评论

      本文标题:replaceAll 兼容性问题

      本文链接:https://www.haomeiwen.com/subject/phcnbrtx.html