1.js中的replaceAll实现

string.replace(/reallyDo/g, replaceWith);
string.replace(new RegExp(reallyDo, 'g'), replaceWith);

string:字符串表达式包含要替代的子字符串。
reallyDo:被搜索的子字符串。
replaceWith:用于替换的子字符串。
复制代码 代码如下:

<script type="text/javascript"> 
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); 
    } 
} 
</script>  

 

2.

相关文章:

  • 2021-10-26
  • 2022-12-23
  • 2021-09-05
  • 2022-02-05
  • 2021-05-23
  • 2022-12-23
猜你喜欢
  • 2021-10-07
  • 2022-02-07
  • 2021-05-18
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
相关资源
相似解决方案