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 相关文章推荐
网站页面自动跳转实现方法PHP、JSP(上)
Aug 01 Javascript
CSS和JS标签style属性对照表(方便js开发的朋友)
Nov 11 Javascript
JavaScript 学习笔记之一jQuery写法图片等比缩放以及预加载
Jun 28 Javascript
extjs表格文本启用选择复制功能具体实现
Oct 11 Javascript
CSS3,HTML5和jQuery搜索框集锦
Dec 02 Javascript
node.js中的path.sep方法使用说明
Dec 08 Javascript
jQuery插件实现大图全屏图片相册
Mar 14 Javascript
jquery实现页面常用的返回顶部效果
Mar 04 Javascript
浅谈JavaScript中变量和函数声明的提升
Aug 09 Javascript
Angular 2应用的8个主要构造块有哪些
Oct 17 Javascript
JavaScript实现清空(重置)文件类型INPUT元素值的方法
Nov 17 Javascript
vue+axios实现文件下载及vue中使用axios的实例
Sep 21 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
从MySQL数据库表中取出随机数据的代码
2007/09/05 PHP
PHP连接局域网MYSQL数据库的简单实例
2013/08/26 PHP
php基本函数汇总
2015/07/09 PHP
juqery 学习之五 文档处理 插入
2011/02/11 Javascript
js toFixed()方法的重写实现精度的统一
2014/03/06 Javascript
jquery查找父元素、子元素(个人经验总结)
2014/04/09 Javascript
轻松掌握JavaScript中的Math object数学对象
2016/05/26 Javascript
bootstrap网格系统使用方法解析
2017/01/13 Javascript
canvas实现流星雨的背景效果
2017/01/13 Javascript
微信小程序 rich-text的使用方法
2017/08/04 Javascript
javascript 中事件冒泡和事件捕获机制的详解
2017/09/01 Javascript
Angular4学习之Angular CLI的安装与使用教程
2018/01/04 Javascript
vue router 通过路由来实现切换头部标题功能
2019/04/24 Javascript
在layui中select更改后生效的方法
2019/09/05 Javascript
简单了解JavaScript作用域
2020/07/31 Javascript
从零学python系列之新版本导入httplib模块报ImportError解决方案
2014/05/23 Python
Python实现遍历windows所有窗口并输出窗口标题的方法
2015/03/13 Python
Python中join和split用法实例
2015/04/14 Python
Python编程实现两个文件夹里文件的对比功能示例【包含内容的对比】
2017/06/20 Python
Python制作词云的方法
2018/01/03 Python
TensorFlow如何实现反向传播
2018/02/06 Python
Python反射的用法实例分析
2018/02/11 Python
python实现微信自动回复机器人功能
2019/07/11 Python
python之生成多层json结构的实现
2020/02/27 Python
TensorFlow keras卷积神经网络 添加L2正则化方式
2020/05/22 Python
加拿大消费电子和手机购物网站:The Source
2017/01/28 全球购物
澳大利亚首个在线预订旅游网站:Wotif
2017/07/19 全球购物
大学国际贸易专业自荐信
2014/06/05 职场文书
数控专业毕业生求职信
2014/06/12 职场文书
企业党员个人自我评价
2014/09/20 职场文书
群众路线对照检查材料
2014/09/22 职场文书
护理工作个人总结
2015/03/03 职场文书
我在伊朗长大观后感
2015/06/16 职场文书
《金钱的魔力》教学反思
2016/02/20 职场文书
python执行js代码的方法
2021/05/13 Python
漫画「古见同学有交流障碍症」第25卷封面公开
2022/03/21 日漫