JavaScript 中的replace方法說(shuō)明
更新時(shí)間:2007年04月13日 00:00:00 作者:
第一次發(fā)現(xiàn)JavaScript中replace() 方法如果直接用str.replace("-","!") 只會(huì)替換第一個(gè)匹配的字符.
而str.replace(/\-/g,"!")則可以替換掉全部匹配的字符(g為全局標(biāo)志)。
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!”
而str.replace(/\-/g,"!")則可以替換掉全部匹配的字符(g為全局標(biāo)志)。
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!”
相關(guān)文章
js運(yùn)動(dòng)應(yīng)用實(shí)例解析
這篇文章主要針對(duì)js運(yùn)動(dòng)應(yīng)用進(jìn)行實(shí)例解析2015-12-12
JavaScript實(shí)現(xiàn)輪播圖效果代碼實(shí)例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)輪播圖效果代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
理解JavaScript設(shè)計(jì)模式中的單例模式
這篇文章主要介紹了理解JavaScript設(shè)計(jì)模式中的單例模式,單例模式即Singleton?Pattern是最簡(jiǎn)單的設(shè)計(jì)模式之一,下文更多相關(guān)介紹感興趣的小伙伴可以參考一下2022-04-04
javascript的鍵盤(pán)控制事件說(shuō)明
獲取鍵盤(pán)控制事件是實(shí)現(xiàn)交互性最有力的方法之一。2008-04-04
動(dòng)態(tài)顯示可輸入的字?jǐn)?shù)提示還可以輸入的字?jǐn)?shù)
這篇文章主要介紹了動(dòng)態(tài)顯示可輸入的字?jǐn)?shù)提示還可以輸入的字?jǐn)?shù),需要的朋友可以參考下2014-04-04

