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 相关文章推荐
文本框输入时 实现自动提示(像百度、google一样)
Apr 05 Javascript
高性能Javascript笔记 数据的存储与访问性能优化
Aug 02 Javascript
检查表单元素的值是否为空的实例代码
Jun 16 Javascript
基于jQuery实现中英文切换导航条效果
Sep 18 Javascript
Bootstrap3 多个模态对话框无法显示的解决方案
Feb 23 Javascript
使用yeoman构建angular应用的方法
Aug 14 Javascript
js实现音乐播放控制条
Sep 09 Javascript
使用nvm管理不同版本的node与npm的方法
Oct 31 Javascript
JS如何调用WebAssembly编译出来的.wasm文件
Nov 05 Javascript
vue element实现表格合并行数据
Nov 30 Vue.js
javascript实现用户必须勾选协议实例讲解
Mar 24 Javascript
elementui的el-popover修改样式不生效的解决
Jun 30 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
PHP判断搜索引擎蜘蛛并自动记忆到文件的代码
2012/02/04 PHP
PHP版微信公众平台红包API
2015/04/02 PHP
PHP根据session与cookie用户登录状态操作类的代码
2016/05/13 PHP
PHP数据对象PDO操作技巧小结
2016/09/27 PHP
yii2 commands模式以及配置crontab定时任务的方法
2017/08/19 PHP
laravel 出现command not found问题的解决方案
2019/10/23 PHP
js传值 判断
2006/10/26 Javascript
JAVASCRIPT对象及属性
2007/02/13 Javascript
Javascript模块模式分析
2008/05/16 Javascript
WEB 浏览器兼容 推荐收藏
2010/05/14 Javascript
jQuery创建平滑的页面滚动(顶部或底部)
2013/02/26 Javascript
Js nodeType 属性全面解析
2013/11/14 Javascript
JS获取URL中的参数数据
2013/12/05 Javascript
AngularJS实用开发技巧(推荐)
2016/07/13 Javascript
js css自定义分页效果
2017/02/24 Javascript
微信小程序 弹框和模态框实现代码
2017/03/10 Javascript
Vue2.5通过json文件读取数据的方法
2018/02/27 Javascript
解决vue-router进行build无法正常显示路由页面的问题
2018/03/06 Javascript
JavaScript两种计时器的实例讲解
2019/01/31 Javascript
javascript面向对象创建对象的方式小结
2019/07/29 Javascript
小程序如何支持使用 async/await详解
2019/09/12 Javascript
JS实现页面鼠标点击出现图片特效
2020/08/19 Javascript
vue实现一个获取按键展示快捷键效果的Input组件
2021/01/13 Vue.js
TensorFlow实现AutoEncoder自编码器
2018/03/09 Python
Python中字符串String的基本内置函数与过滤字符模块函数的基本用法
2019/05/27 Python
python 3.7.4 安装 opencv的教程
2019/10/10 Python
python3安装OCR识别库tesserocr过程图解
2020/04/02 Python
TensorFlow保存TensorBoard图像操作
2020/06/23 Python
h5实现获取用户地理定位的实例代码
2017/07/17 HTML / CSS
监理资料员岗位职责
2014/01/03 职场文书
民警个人对照检查剖析材料
2014/09/17 职场文书
招商银行工作证明
2015/06/17 职场文书
重阳节主题班会
2015/08/17 职场文书
python 如何做一个识别率百分百的OCR
2021/05/29 Python
win10滚动条自动往上跑怎么办?win10滚动条自动往上跑的解决方法
2022/08/05 数码科技
Python中np.random.randint()参数详解及用法实例
2022/09/23 Python