JavaScript 中的replace方法说明


Posted in Javascript onApril 13, 2007

第一次发现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!”  

Javascript 相关文章推荐
扩展String功能方法
Sep 22 Javascript
IE8 chrome中table隔行换色解决办法
Jul 09 Javascript
学习JavaScript设计模式之代理模式
Jan 12 Javascript
基于JavaScript实现瀑布流效果(循环渐近)
Jan 27 Javascript
jQuery用FormData实现文件上传的方法
Nov 21 Javascript
js实现简单的获取验证码按钮效果
Mar 03 Javascript
JS去掉字符串前后空格或去掉所有空格的用法
Mar 25 Javascript
JavaScript数据结构之广义表的定义与表示方法详解
Apr 12 Javascript
Vue 项目分环境打包的方法示例
Aug 03 Javascript
Vue入门之数量加减运算操作示例
Dec 11 Javascript
微信内置开发 iOS修改键盘换行为搜索的解决方案
Nov 06 Javascript
微信小程序 scroll-view的使用案例代码详解
Jun 11 Javascript
深入聊聊Array的sort方法的使用技巧.详细点评protype.js中的sortBy方法
Apr 12 #Javascript
firefox 和 ie 事件处理的细节,研究,再研究 书写同时兼容ie和ff的事件处理代码
Apr 12 #Javascript
使用prototype.js 的时候应该特别注意的几个问题.
Apr 12 #Javascript
讲两件事:1.this指针的用法小探. 2.ie的attachEvent和firefox的addEventListener在事件处理上的区别
Apr 12 #Javascript
ie 处理 gif动画 的onload 事件的一个 bug
Apr 12 #Javascript
无语,javascript居然支持中文(unicode)编程!
Apr 12 #Javascript
几个高效,简洁的字符处理函数
Apr 12 #Javascript
You might like
咖啡历史、消费和行业趋势
2021/03/03 咖啡文化
php实现文件上传及头像预览功能
2017/01/15 PHP
PHP判断访客是否手机端(移动端浏览器)访问的方法总结【4种方法】
2019/03/27 PHP
Extjs学习笔记之三 extjs form更多的表单项
2010/01/07 Javascript
javascript的数据类型、字面量、变量介绍
2012/05/23 Javascript
关于JS判断图片是否加载完成且获取图片宽度的方法
2013/04/09 Javascript
jQuery随机切换图片的小例子
2013/04/18 Javascript
JS修改css样式style浅谈
2013/05/06 Javascript
kindeditor修复会替换script内容的问题
2015/04/03 Javascript
JS日程管理插件FullCalendar简单实例
2017/02/07 Javascript
jQuery操作之效果详解
2017/05/19 jQuery
浅谈vue中数据双向绑定的实现原理
2017/09/14 Javascript
浅谈react-native热更新react-native-pushy集成遇到的问题
2017/09/30 Javascript
three.js 入门案例详解
2018/01/23 Javascript
VUE DOM加载后执行自定义事件的方法
2018/09/07 Javascript
微信小程序在text文本实现多种字体样式
2019/11/08 Javascript
详解Python中的__init__和__new__
2014/03/12 Python
详解Python多线程Selenium跨浏览器测试
2017/04/01 Python
浅谈django开发者模式中的autoreload是如何实现的
2017/08/18 Python
理解python中生成器用法
2017/12/20 Python
python查看模块安装位置的方法
2018/10/16 Python
python 实现两个线程交替执行
2020/05/02 Python
Python2手动安装更新pip过程实例解析
2020/07/16 Python
使用Python封装excel操作指南
2021/01/29 Python
详解CSS3中强大的filter(滤镜)属性
2017/06/29 HTML / CSS
html5新增的属性和废除的属性简要概述
2013/02/20 HTML / CSS
StubHub巴西:购买和出售您的门票
2016/07/22 全球购物
主持人婚宴答谢词
2014/01/28 职场文书
网吧消防安全制度
2014/01/28 职场文书
音乐教学反思
2014/02/02 职场文书
汽车广告策划方案
2014/05/31 职场文书
服务明星事迹材料
2014/12/29 职场文书
普通员工辞职信范文
2015/05/12 职场文书
中考百日冲刺决心书
2015/09/22 职场文书
python 闭包函数详细介绍
2022/04/19 Python
Python 读取千万级数据自动写入 MySQL 数据库
2022/06/28 Python