js常用技巧

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.

原文地址:https://www.cnblogs.com/PheonixHkbxoic/p/5920611.html