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");

 

posted @ 2015-12-16 17:22  龙昭羽  阅读(309)  评论(0编辑  收藏  举报