javascript中使用replaceAll()函数实现字符替换的方法


Posted in Javascript onDecember 25, 2010

而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脚本语言在网页中的简单应用
May 13 Javascript
让GoogleCode的SVN下的HTML文件在FireFox下正常显示.
May 25 Javascript
javascript 兼容各个浏览器的事件
Feb 04 Javascript
jquery仿百度经验滑动切换浏览效果
Apr 14 Javascript
js简单实现竖向tab选项卡的方法
May 04 Javascript
RequireJS入门一之实现第一个例子
Sep 30 Javascript
JS中script标签defer和async属性的区别详解
Aug 12 Javascript
JS及JQuery对Html内容编码,Html转义
Feb 17 Javascript
jQuery animate()实现背景色渐变效果的处理方法【使用jQuery.color.js插件】
Mar 15 Javascript
vue中的模态对话框组件实现过程
May 01 Javascript
使用Vue+Django+Ant Design做一个留言评论模块的示例代码
Jun 01 Javascript
vue中的可拖拽宽度div的实现示例
Apr 08 Vue.js
Javascript动态绑定事件的简单实现代码
Dec 25 #Javascript
浅析javascript闭包 实例分析
Dec 25 #Javascript
父子窗体间传递JSON格式的数据的代码
Dec 25 #Javascript
javascript自执行函数之伪命名空间封装法
Dec 25 #Javascript
Ext对基本类型的扩展 ext,extjs,format
Dec 25 #Javascript
JQuery live函数
Dec 24 #Javascript
jquery 单击li防止重复加载的实现代码
Dec 24 #Javascript
You might like
一篇入门的php Class 文章
2007/04/04 PHP
php checkdate、getdate等日期时间函数操作详解
2010/03/11 PHP
基于MySQL到MongoDB简易对照表的详解
2013/06/03 PHP
php使浏览器直接下载pdf文件的方法
2013/11/15 PHP
php结合正则批量抓取网页中邮箱地址
2015/05/19 PHP
phpcms中的评论样式修改方法
2016/10/21 PHP
ThinkPHP3.2框架自定义配置和加载用法示例
2018/06/14 PHP
ThinkPHP框架整合微信支付之刷卡模式图文详解
2019/04/10 PHP
Laravel框架数据库迁移操作实例详解
2020/04/06 PHP
IE php关于强制下载文件的代码
2008/08/23 Javascript
js substr、substring和slice使用说明小记
2011/09/15 Javascript
刷新页面的几种方法小结(JS,ASP.NET)
2014/01/07 Javascript
SeaJS入门教程系列之SeaJS介绍(一)
2014/03/03 Javascript
ES6新特性六:promise对象实例详解
2017/04/21 Javascript
深入理解vue-router之keep-alive
2017/08/31 Javascript
axios post提交formdata的实例
2018/03/16 Javascript
layui多iframe页面控制定时器运行的方法
2019/09/05 Javascript
webpack5 联邦模块介绍详解
2020/07/08 Javascript
vue数据更新UI不刷新显示的解决办法
2020/08/06 Javascript
[04:42]5分钟带你了解什么是DOTA2(第一期)
2017/02/07 DOTA
python 3利用BeautifulSoup抓取div标签的方法示例
2017/05/28 Python
python钉钉机器人运维脚本监控实例
2019/02/20 Python
PythonWeb项目Django部署在Ubuntu18.04腾讯云主机上
2019/04/01 Python
python-tornado的接口用swagger进行包装的实例
2019/08/29 Python
python Manager 之dict KeyError问题的解决
2019/12/21 Python
浅析python中的del用法
2020/09/02 Python
python3.9.1环境安装的方法(图文)
2021/02/02 Python
html5 canvas fillRect坐标和大小的问题解决方法
2014/03/26 HTML / CSS
全球性的在线时尚男装零售商:boohooMAN
2016/12/17 全球购物
美国正宗设计师眼镜在线零售商:EYEZZ
2019/03/23 全球购物
灰雀教学反思
2014/04/28 职场文书
2014党的群众路线教育实践活动总结报告
2014/10/31 职场文书
夫妻忠诚协议书范本
2014/11/17 职场文书
2015年小学教导处工作总结
2015/05/26 职场文书
公司费用报销管理制度
2015/08/04 职场文书
mysql sql常用语句大全
2022/06/21 MySQL