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

Javascript
经常用到的JavasScript事件的翻译
Javascript下的keyCode键码值表
用javascript动态调整iframe高度的代码
javascript应用:Iframe自适应其加载的内容高度
javascript 控制弹出窗口
用javascript父窗口控制只弹出一个子窗口
给moz-firefox下添加IE方法和属性
FireFox中textNode分片的问题
对google个性主页的拖拽效果的js的完整注释[转]
身份证号码前六位所代表的省,市,区, 以及地区编码下载
一个友好的.改善的 Object.prototype.toString的实现
如何写一个通用的JavaScript效果库!(1/2)
如何写一个通用的JavaScript效果库!(2/2)
几个高效,简洁的字符处理函数
无语,javascript居然支持中文(unicode)编程!
ie 处理 gif动画 的onload 事件的一个 bug
讲两件事:1.this指针的用法小探. 2.ie的attachEvent和firefox的addEventListener在事件处理上的区别
firefox 和 ie 事件处理的细节,研究,再研究-----书写同时兼容ie和ff的事件处理代码
深入聊聊Array的sort方法的使用技巧.详细点评protype.js中的sortBy方法
JavaScript 中的replace方法说明

Javascript 中的 JavaScript 中的replace方法说明


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