手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >JavaScript 中的replace方法说明
JavaScript 中的replace方法说明
摘要:第一次发现JavaScript中replace()方法如果直接用str.replace("-","!")只会替换第一个匹配的字符.而str....

第一次发现JavaScript中replace()方法如果直接用str.replace("-","!")只会替换第一个匹配的字符.

而str.replace(/-/g,"!")则可以替换掉全部匹配的字符(g为全局标志)。

replace()

Thereplace()methodreturnsthestringthatresultswhenyoureplacetextmatchingitsfirstargument

(aregularexpression)withthetextofthesecondargument(astring).

Iftheg(global)flagisnotsetintheregularexpressiondeclaration,thismethodreplacesonlythefirst

occurrenceofthepattern.Forexample,

vars="Hello.Regexpsarefun.";s=s.replace(/./,"!");//replacefirstperiodwithanexclamationpointalert(s);

producesthestring“Hello!Regexpsarefun.”Includingthegflagwillcausetheinterpreterto

performaglobalreplace,findingandreplacingeverymatchingsubstring.Forexample,

vars="Hello.Regexpsarefun.";s=s.replace(/./g,"!");//replaceallperiodswithexclamationpointsalert(s);

yieldsthisresult:“Hello!Regexpsarefun!”

【JavaScript 中的replace方法说明】相关文章:

JavaScript实现鼠标滑过处生成气泡的方法

JavaScript中Number.MAX_VALUE属性的使用方法

JQuery中clone方法复制节点

javaScript中push函数用法实例分析

JavaScript实现鼠标点击后层展开效果的方法

详解 JavaScript 闭包的小知识

JavaScript判断图片是否已经加载完毕的方法汇总

javascript用函数实现对象的方法

JavaScript中的splice()方法使用详解

详解JavaScript中setSeconds()方法的使用

精品推荐
分类导航