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

replace()
The replace() method retur " /> 日韩欧美国产精品综合嫩v,亚洲精品电影,青青一区二区三区

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

JavaScript 中的replace方法說明

第一次發現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!”  

JavaScript技術JavaScript 中的replace方法說明,轉載需保留來源!

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

主站蜘蛛池模板: 水城县| 神农架林区| 平江县| 辛集市| 太和县| 兴安县| 河源市| 将乐县| 通州区| 易门县| 长春市| 渝北区| 铜陵市| 怀集县| 洛隆县| 汤原县| 尼玛县| 星子县| 保定市| 巴彦淖尔市| 陆丰市| 黔江区| 华池县| 上饶县| 巴彦淖尔市| 利津县| 南开区| 岐山县| 永靖县| 南木林县| 蓬安县| 平遥县| 邢台市| 宕昌县| 澄城县| 棋牌| 北辰区| 谢通门县| 宜君县| 马山县| 信阳市|