当前位置: 首页 > 图文教程 > 网络编程 > Javascript > JavaScript 中的replace方法说明

Javascript
非常厉害的javascript 实现指针式时间
文档处理系列——随时更新
用javascript实现文本框和"选择"按扭之间的间距
使用户点击后退按钮使效三行代码
Javascript 个人笔记(没有整理,很乱)
建议大家看下JavaScript重要知识更新
javascript实现操作cookie实现的可记忆菜单
javascript下for循环用法小结
一个快速添加标签的小玩意,但在FF下不能用,望高手指点
HTML中事件触发列表与解说
极品源码:自动完成 仿163篇
doctype后如何获得body.clientHeight的方法
用javascript调出windows色版的代码
js实现的网页颜色代码表全集
javascript实现爱你在FF IE下都有效的添加一个项目
js实现用滚动条来放大缩小图片的代码
javascript实现unicode和字符的互相转换
符合W3C网页标准的iframe标签的使用方法
firefox下javascript实现高亮关键词的方法
用javascript实现读取txt文档的脚本

Javascript 中的 JavaScript 中的replace方法说明


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 123 ::
收藏到网摘: n/a

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