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入门教程(6) Window窗口对象
Jan 31 Javascript
JS获取dom 对象 ajax操作 读写cookie函数
Nov 18 Javascript
javascript 面向对象全新理练之数据的封装
Dec 03 Javascript
js获取本机的外网/广域网ip地址完整源码
Aug 12 Javascript
JavaScript阻止浏览器返回按钮的方法
Mar 18 Javascript
JavaScript实现将文本框的值插入指定位置的方法
Aug 13 Javascript
jQuery实现响应鼠标背景变化的动态菜单效果代码
Aug 27 Javascript
针对JavaScript中this指向的简单理解
Aug 26 Javascript
xmlplus组件设计系列之网格(DataGrid)(10)
May 05 Javascript
JavaScript实现各种排序的代码详解
Aug 28 Javascript
使用JS判断页面是首次被加载还是刷新
May 26 Javascript
不刷新网页就能链接新的js文件方法总结
Mar 01 Javascript
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
IIS6.0 开启Gzip方法及PHP Gzip函数分享
2014/06/08 PHP
php实现session自定义会话处理器的方法
2015/01/27 PHP
PHP微信开发之二维码生成类
2015/06/26 PHP
PHP实现登录验证码校验功能
2018/05/17 PHP
一个很简单的办法实现TD的加亮效果.
2006/06/29 Javascript
javascript第一课
2007/02/27 Javascript
JQuery SELECT单选模拟jQuery.select.js
2009/11/12 Javascript
javascript中的toFixed固定小数位数 简单实例分享
2013/07/12 Javascript
javascript中Date对象的getDay方法使用指南
2014/12/22 Javascript
AngularJS控制器之间的数据共享及通信详解
2016/08/01 Javascript
微信小程序 教程之列表渲染
2016/10/18 Javascript
JS实现“隐藏与显示”功能(多种方法)
2016/11/24 Javascript
Bootstrap源码解读排版(1)
2016/12/23 Javascript
Vue keep-alive实践总结(推荐)
2017/08/31 Javascript
js实现rem自动匹配计算font-size的示例
2017/11/18 Javascript
IE11下CKEditor在Bootstrap Modal中下拉问题的解决
2019/09/25 Javascript
OpenLayers3实现地图显示功能
2020/09/25 Javascript
详解ES6实现类的私有变量的几种写法
2021/02/10 Javascript
python 平衡二叉树实现代码示例
2018/07/07 Python
Python子类继承父类构造函数详解
2019/02/19 Python
Pycharm如何打断点的方法步骤
2019/06/13 Python
python笔记之mean()函数实现求取均值的功能代码
2019/07/05 Python
Scrapy-Redis之RedisSpider与RedisCrawlSpider详解
2020/11/18 Python
Python约瑟夫生者死者小游戏实例讲解
2021/01/04 Python
Viking Direct爱尔兰:办公用品和家具
2019/11/21 全球购物
俄罗斯GamePark游戏商店网站:购买游戏、游戏机和配件
2020/03/13 全球购物
乌克兰设计师和品牌的服装:Love&Live
2020/04/14 全球购物
英语系毕业生自荐信
2013/10/31 职场文书
公司总经理岗位职责
2014/03/15 职场文书
三方合作协议书范本
2014/04/18 职场文书
病人写给医生的感谢信
2015/01/23 职场文书
2015年度合同管理工作总结
2015/05/22 职场文书
党务工作者主要事迹材料
2015/11/03 职场文书
教您怎么制定西餐厅运营方案 ?
2019/07/05 职场文书
MySQL 8.0 Online DDL快速加列的相关总结
2021/06/02 MySQL
Vue2项目中对百度地图的封装使用详解
2022/06/16 Vue.js