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 相关文章推荐
jQuery Ajax使用 全解析
Dec 15 Javascript
js判断是否为数组的函数: isArray()
Oct 30 Javascript
jquery判断至少有一个checkbox被选中的方法
Jun 05 Javascript
在easyUI开发中,出现jquery.easyui.min.js函数库问题的解决办法
Sep 11 Javascript
简介BootStrap model弹出框的使用
Apr 27 Javascript
JavaScript之DOM_动力节点Java学院整理
Jul 03 Javascript
Vue实现数字输入框中分割手机号码的示例
Oct 10 Javascript
动态加载、移除js/css文件的示例代码
Mar 20 Javascript
用vue快速开发app的脚手架工具
Jun 11 Javascript
Node.js从字符串生成文件流的实现方法
Aug 18 Javascript
解决Layui中layer报错的问题
Sep 03 Javascript
微信小程序实现拨打电话功能的示例代码
Jun 28 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
DW中链接mysql数据库时,建立字符集中文出现乱码的解决方法
2010/03/27 PHP
发一个php简单的伪原创程序,配合商城采集用的
2010/10/12 PHP
PHP 利用AJAX获取网页并输出的实现代码(Zjmainstay)
2012/08/31 PHP
php源码分析之DZX1.5随机数函数random用法
2015/06/17 PHP
PHP结合Mysql数据库实现留言板功能
2016/03/04 PHP
非集成环境的php运行环境(Apache配置、Mysql)搭建安装图文教程
2016/04/12 PHP
php实现批量修改文件名称的方法
2016/07/23 PHP
表单的焦点顺序tabindex和对应enter键提交
2013/01/04 Javascript
JavaScript将字符串转换为整数的方法
2015/04/14 Javascript
BootStrap Datetimepicker 汉化的实现代码
2017/02/10 Javascript
Vue.js原理分析之observer模块详解
2017/02/17 Javascript
jQuery插件ContextMenu自定义图标
2017/03/15 Javascript
vue.js 使用v-if v-else发现没有执行解决办法
2017/05/15 Javascript
Vue组件通信实践记录(推荐)
2017/08/15 Javascript
zTree树形菜单交互选项卡效果的实现方法
2017/12/25 Javascript
Vue filter 过滤器、以及在table中的使用介绍
2020/09/07 Javascript
[13:40]TI3青蛙君全程回顾 DOTA2我们为梦想再战
2013/09/13 DOTA
[06:13]DOTA2进化论(修改版)
2013/10/08 DOTA
[01:18]PWL开团时刻DAY10——一拳超人
2020/11/11 DOTA
从零学Python之hello world
2014/05/21 Python
Python用imghdr模块识别图片格式实例解析
2018/01/11 Python
Python一句代码实现找出所有水仙花数的方法
2018/11/13 Python
对python当中不在本路径的py文件的引用详解
2018/12/15 Python
python 如何去除字符串头尾的多余符号
2019/11/19 Python
Python使用循环神经网络解决文本分类问题的方法详解
2020/01/16 Python
python发qq消息轰炸虐狗好友思路详解(完整代码)
2020/02/15 Python
python 通过文件夹导入包的操作
2020/06/01 Python
python pygame 愤怒的小鸟游戏示例代码
2021/02/25 Python
mysql有关权限的表都有哪几个
2015/04/22 面试题
个人求职简历的自我评价范文
2013/10/09 职场文书
农救科工作职责
2013/11/27 职场文书
自主招生自荐信范文
2013/12/04 职场文书
社区工作者思想汇报
2014/01/13 职场文书
实验教师岗位职责
2014/02/13 职场文书
安全口号大全
2014/06/21 职场文书
党的群众路线教育实践活动领导班子整改措施
2014/09/30 职场文书