JS中实现replaceAll的方法(实例代码)


Posted in Javascript onNovember 12, 2013

第一次发现JavaScript中replace() 方法如果直接用str.replace("-","!") 只会替换第一个匹配的字符.
而str.replace(/\-/g,"!")则可以全部替换掉匹配的字符(g为全局标志)。

replace()
The replace() method returns the string that results when you replace text matching its first argument
(a regular expression) with the text of the second argument (a string).
If the g (global) flag is not set in the regular expression declaration, this method replaces only the first
occurrence of the pattern. For example,

var s = "Hello. Regexps are fun.";s = s.replace(/\./, "!"); // replace first period with an exclamation pointalert(s);

produces the string “Hello! Regexps are fun.” Including the g flag will cause the interpreter to
perform a global replace, finding and replacing every matching substring. For example,

var s = "Hello. Regexps are fun.";s = s.replace(/\./g, "!"); // replace all periods with exclamation pointsalert(s);

yields this result: “Hello! Regexps are fun!”

所以可以用以下几种方式.:
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> 
Javascript 相关文章推荐
javascript下查找父节点的简单方法
Aug 13 Javascript
jquery创建表格(自动增加表格)代码分享
Dec 25 Javascript
javascript中的3种继承实现方法
Jan 27 Javascript
第五章之BootStrap 栅格系统
Apr 25 Javascript
angular和BootStrap3实现购物车功能
Jan 25 Javascript
Vue.js 2.0 移动端拍照压缩图片上传预览功能
Mar 06 Javascript
js实现三级联动效果(简单易懂)
Mar 27 Javascript
JavaScript 通过Ajax 动态加载CheckBox复选框
Aug 31 Javascript
一个基于react的图片裁剪组件示例
Apr 18 Javascript
jQuery pagination分页示例详解
Oct 23 jQuery
微信小程序登录按钮遮罩浮层效果的实现方法
Dec 16 Javascript
vue-axios同时请求多个接口 等所有接口全部加载完成再处理操作
Nov 09 Javascript
jquery.validate的使用说明介绍
Nov 12 #Javascript
javascript 函数及作用域总结介绍
Nov 12 #Javascript
Javascript之this关键字深入解析
Nov 12 #Javascript
js hover 定时器(实例代码)
Nov 12 #Javascript
JavaScript的setAttribute兼容性问题解决方法
Nov 11 #Javascript
javascript实用小函数使用介绍
Nov 11 #Javascript
JavaScript动态插入script的基本思路及实现函数
Nov 11 #Javascript
You might like
PHP编码规范之注释和文件结构说明
2010/07/09 PHP
获取用户Ip地址通用方法与常见安全隐患(HTTP_X_FORWARDED_FOR)
2013/06/01 PHP
教你如何用php实现LOL数据远程获取
2014/06/10 PHP
php实现的SESSION类
2014/12/02 PHP
PHP IDE PHPStorm配置支持友好Laravel代码提示方法
2015/05/12 PHP
php如何执行非缓冲查询API
2016/07/22 PHP
jQuery autocomplete插件修改
2009/04/17 Javascript
Jquery 组合form元素为json格式,asp.net反序列化
2009/07/09 Javascript
IE不支持getElementsByClassName最终完美解决方案
2012/12/17 Javascript
js 动态修改css文件的方法
2014/08/05 Javascript
Json实现异步请求提交评论无需跳转其他页面
2014/10/11 Javascript
Node.js模块封装及使用方法
2016/03/06 Javascript
jQuery实现鼠标滚动图片延迟加载效果附源码下载
2016/06/28 Javascript
利用JS实现简单的日期选择插件
2017/01/23 Javascript
javascript帧动画(实例讲解)
2017/09/02 Javascript
10个经典的网页鼠标特效代码
2018/01/09 Javascript
使用RN Animated做一个“添加购物车”动画的方法
2018/09/12 Javascript
electron中使用bootstrap的示例代码
2018/11/06 Javascript
详解key在Vue列表渲染时究竟起到了什么作用
2019/04/20 Javascript
JS使用Chrome浏览器实现调试线上代码
2020/07/23 Javascript
[01:27]2014DOTA2展望TI 剑指西雅图IG战队专访
2014/06/30 DOTA
Python实现的飞速中文网小说下载脚本
2015/04/23 Python
python中管道用法入门实例
2015/06/04 Python
浅谈python中截取字符函数strip,lstrip,rstrip
2015/07/17 Python
Python定义一个函数的方法
2020/06/15 Python
快速解释如何使用pandas的inplace参数的使用
2020/07/23 Python
英国知名化妆品网站:Revolution Beauty(原TAM Beauty)
2018/02/28 全球购物
联想德国官网:Lenovo Germany
2018/07/04 全球购物
美国Curacao百货连锁店网站:iCuracao.com
2019/07/20 全球购物
Wiggle新西兰:自行车、跑步、游泳
2020/05/06 全球购物
关于递归的一道.NET面试题
2013/05/12 面试题
公司端午节活动方案
2014/02/04 职场文书
售后服务承诺书怎么写
2014/05/21 职场文书
晚会开场白和结束语
2015/05/29 职场文书
体育教师研修感悟
2015/11/18 职场文书
教师实习自我鉴定总结
2019/08/20 职场文书