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

replace()
The replace() method retur " /> 久久精品国产久精国产爱,91色在线看,国产精品久久777777毛茸茸

色尼玛亚洲综合影院,亚洲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方法說明,轉載需保留來源!

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

主站蜘蛛池模板: 博白县| 仲巴县| 白玉县| 旌德县| 宁阳县| 昔阳县| 安国市| 东乌珠穆沁旗| 临泽县| 江孜县| 晋城| 巫溪县| 巴南区| 邮箱| 宝应县| 新竹市| 巫溪县| 乌什县| 常熟市| 奉新县| 宜黄县| 佛坪县| 云南省| 揭阳市| 北碚区| 长乐市| 定南县| 榆树市| 玉龙| 新邵县| 溧水县| 克山县| 沙田区| 砀山县| 井陉县| 竹溪县| 鲜城| 射阳县| 武定县| 广州市| 敦化市|