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

Javascript
Javascript-Mozilla和IE中的一个函数直接量的问题分析
TopList标签和JavaScript结合两例
javascript-TreeView父子联动效果保持节点状态一致
不错的JS中变量相关的细节分析
解决使用attachEvent函数时,this指向被绑定的元素的问题的方法
根据地区不同显示时间的javascript代码
javascript下查找父节点的简单方法
分享别人写的一个小型js框架
封装好的省市地区联动控件附下载
权威JavaScript 中的内存泄露模式
JavaScript 编程引入命名空间的方法与代码
IE/FireFox具备兼容性的拖动代码
分析 JavaScript 中令人困惑的变量赋值
推荐一个小巧的JS日历
让回调函数 showResponse 也带上参数的代码
增强的 JavaScript 的 trim 函数的代码
高亮闪烁某个元素的js脚本
用JavaScript 处理 URL 的两个函数代码
JavaScript 模仿vbs中的 DateAdd() 函数的代码
用js实现随机返回数组的一个元素

Javascript 中的 JavaScript 中的replace方法说明


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 127 ::
收藏到网摘: 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!”