js常用技巧

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.

以上是关于js常用技巧的主要内容,如果未能解决你的问题,请参考以下文章

js常用代码片段(更新中)

js常用代码片段

javascript JS-常用代码片段

js 常用代码片段

前端开发常用js代码片段

JS常用代码片段2-值得收藏