网上的:

String.prototype.replaceAll = function(str1, str2) {
    var str = this;
    var result = str.replace(eval("/" + str1 + "/gi"), str2);
    return result;
}

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

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

这几种其实在特殊字符时无效

  String.prototype.replaceAll = function (oldStr, newStr) {
            var content = this;
            while (content.indexOf(oldStr) >= 0) {
                content = content.replace(oldStr, newStr);
            }
            return content;
        }
   var val = "[red][b]&lt;script&gt;alert('asdfsd');&lt;/script&gt;[/b][/red]<br>";
        val = val.replaceAll('[b]', '<b>'); 
        val = val.replaceAll('[/b]', '</b>'); 
        val = val.replaceAll("[red]", "<font color='red'>"); 
        val = val.replaceAll("[/red]", "</font>");

        alert(val);

 

 

相关文章:

  • 2022-12-23
  • 2022-02-17
  • 2021-11-17
  • 2022-12-23
  • 2021-07-30
  • 2021-11-06
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-05
  • 2022-02-21
  • 2022-12-23
  • 2021-06-25
  • 2022-12-23
  • 2022-12-23
  • 2022-03-01
相关资源
相似解决方案