而str.replace(//-/g,"!")則可以替換掉全部匹配的字符(g為全局標(biāo)志)。

replace()
The replace() method retur " /> 亚洲国产精品va在线观看黑人,а√天堂中文资源在线bt,六月丁香综合

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

JavaScript 中的replace方法說明

第一次發(fā)現(xiàn)JavaScript中replace() 方法如果直接用str.replace("-","!") 只會替換第一個匹配的字符.  
而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!”  

JavaScript技術(shù)JavaScript 中的replace方法說明,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 余干县| 北京市| 忻城县| 改则县| 绥中县| 余姚市| 三门峡市| 肇州县| 左贡县| 玛多县| 韶山市| 仁化县| 乌鲁木齐县| 姜堰市| 高陵县| 伊宁县| 芜湖县| 仁寿县| 武夷山市| 河西区| 株洲县| 加查县| 鹿泉市| 五莲县| 肥城市| 五莲县| 潞城市| 门头沟区| 长治县| 丰顺县| 象山县| 古蔺县| 凤阳县| 宝丰县| 罗城| 赣榆县| 山丹县| 华安县| 孝昌县| 和平县| 沁水县|