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

replace()
The replace() method retur " /> 色呦呦在线资源,日本欧美一区二区三区乱码,中文日韩欧美

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

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

主站蜘蛛池模板: 西青区| 九龙坡区| 德惠市| 福海县| 佛冈县| 临海市| 吴旗县| 淅川县| 五峰| 石嘴山市| 景洪市| 东兴市| 长兴县| 尖扎县| 衡山县| 柳林县| 普定县| 洪泽县| 景洪市| 通海县| 札达县| 六安市| 汝城县| 中江县| 长阳| 普洱| 噶尔县| 洪雅县| 永泰县| 乌兰浩特市| 河津市| 石泉县| 连江县| 弥勒县| 铅山县| 二手房| 新河县| 远安县| 赤城县| 文水县| 建平县|