js replaceAll

js 字符串的replace方法只能替换字符串中匹配的第一个字符串。可以通过自定义replaceAll的方法来替换所有匹配的字符串。

String.prototype.replaceAll = function(s1, s2){
      return this.replace(new RegExp(s1, "gm"),s2);
    }

var str="hello world";
var newStr=str.replaceAll("o","a");
原文地址:https://www.cnblogs.com/longzhaoyu/p/5051749.html