码迷,mamicode.com
首页 > Web开发 > 详细

js常用技巧

时间:2016-09-29 20:53:36      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

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常用技巧

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!