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

Javascript
javascript if 的简化代码
javascript下with 的简化代码写法
javascript简化代码 A=alert w=document.writeln
js 调整select 位置的函数
JS应用之禁止抓屏、复制、打印
js加解密 脚本解密
js+vml创建3D页面效果代码
javascript简写效果“神秘的眼睛”
js实现的定时关闭页面或定时提醒效果代码
js 字数统计,区分英汉
js双色时间效果代码
js计算时间过去的时间
一个javascript参数的小问题
textarea支持图形编辑的实现方法
JavaScript面向对象编程
动态生成的IFRAME,设置SRC时的,不同位置带来的影响
JObj预览一个JS的框架
去除有数组中重复的元素
用JObj实现的渐变效果
给自定义对象加上自定义事件的支持的教程

Javascript 中的 JavaScript 中的replace方法说明


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