javascript中使用replaceAll()函數(shù)實(shí)現(xiàn)字符替換的方法
而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!”
所以可以用以下幾種方式.:
string.replace(/reallyDo/g, replaceWith);
string.replace(new RegExp(reallyDo, 'g'), replaceWith);
string:字符串表達(dá)式包含要替代的子字符串。
reallyDo:被搜索的子字符串。
replaceWith:用于替換的子字符串。
<script type="text/javascript">
String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {
if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);
} else {
return this.replace(reallyDo, replaceWith);
}
}
</script>補(bǔ)充
我們?cè)贘ava中可以使用replaceAll()方法對(duì)字符串進(jìn)行批量替換,但在JS中replaceAll()方法是undefined,JS中只存在replace()方法,因此我們可以自己封裝JS中replaceAll()方法供我們便捷使用。
一、使用replace()方法進(jìn)行替換
定義一個(gè)字符串:
var str = "hello world";
使用replace()方法將字符串中的字母"l"替換成"i",原始做法:
?console.log(str.replace("l","i"));輸出:
“heilo world”
需要執(zhí)行三次,非常不方便;
二、使用replaceAll()方法替換
封裝replaceAll()方法:
String.prototype.replaceAll = function(s1, s2) {
?? ?return this.replace(new RegExp(s1, "gm"), s2);
}定義一個(gè)字符串:
var str = "hello world";
使用replaceAll()方法進(jìn)行批量替換:
console.log(str.replaceAll("l", "i"));輸出:
“heiio worid”
只需要執(zhí)行一次,就完成了全部替換需求。
總結(jié)一下, 四種方式
1. 使用具有全局標(biāo)志g的正則表達(dá)式
var str = "dogdogdog"; var str2 = str.replace(/dog/g,"cat"); console.log(str2);
實(shí)現(xiàn)替換全部匹配字符串,輸出結(jié)果為:catcatcat。
2. 使用另一種具有全局標(biāo)志g的正則表達(dá)式
var str = "dogdogdog";
var str2 = str.replace(new RegExp("dog","gm"),"cat");
console.log(str2);輸出結(jié)果同上例。這里g表示執(zhí)行全局匹配,m表示執(zhí)行多次匹配。
3. 給string對(duì)象添加原型方法replaceAll()
String.prototype.replaceAll = function(s1, s2) {
return this.replace(new RegExp(s1, "gm"), s2);
}這樣就可以像使用replace方法一樣使用replaceAll方法:
var str = "dogdogdog";
var str2 = str.replaceAll("dog", "cat");
console.log(str2);輸出結(jié)果同上例。
4. 使用先split,再join的方法
評(píng)論區(qū)@默默之分享的這個(gè)方法太贊了,拉到正文里以免有人不看評(píng)論,感謝@默默之分享。
var str = "dogdogdog";
var str2 = str.split("dog").join("cat")
console.log(str2);輸出結(jié)果同上例。
- Javascript中正則表達(dá)式的全局匹配模式分析
- Javascript中使用exec進(jìn)行正則表達(dá)式全局匹配時(shí)的注意事項(xiàng)
- JavaScript實(shí)現(xiàn)的字符串replaceAll函數(shù)代碼分享
- javascript實(shí)現(xiàn)全局匹配并替換的方法
- java中replaceAll替換圓括號(hào)實(shí)例代碼
- Java中replace與replaceAll的區(qū)別與測(cè)試
- java字符串的替換replace、replaceAll、replaceFirst的區(qū)別說(shuō)明
- Java replaceAll()方法報(bào)錯(cuò)Illegal group reference的解決辦法
- String.replaceAll方法詳析(正則妙用)
- 淺談Java中replace與replaceAll區(qū)別
- Java中replace、replaceAll和replaceFirst函數(shù)的用法小結(jié)
- 淺談java中replace()和replaceAll()的區(qū)別
- jQuery中replaceAll()方法用法實(shí)例
- js使用正則實(shí)現(xiàn)ReplaceAll全部替換的方法
- js字符串替換所有的指定字符或文字(推薦replaceAll方法)
- JS中實(shí)現(xiàn)replaceAll的方法(實(shí)例代碼)
- js replace 與replaceall實(shí)例用法詳解
- Flex 字符串ReplaceAll使用說(shuō)明
- JavaScript中使用replace結(jié)合正則實(shí)現(xiàn)replaceAll的效果
相關(guān)文章
使用javascript獲取flash加載的百分比的實(shí)現(xiàn)代碼
使用javascript獲取flash加載的百分比的實(shí)現(xiàn)代碼,方便flash小游戲判斷頁(yè)面,提高用戶體驗(yàn)。2011-05-05
JavaScript constructor和instanceof,JSOO中的一對(duì)歡喜冤家
現(xiàn)在流行面向?qū)ο?JavaScript當(dāng)然要迎頭趕上. 有說(shuō)法JavaScript就是徹頭徹尾的OO語(yǔ)言,但我覺得JavaScript實(shí)現(xiàn)面向?qū)ο蟮某绦蜻€是有諸多不便的.2009-05-05
Javascript實(shí)現(xiàn)帶關(guān)閉按鈕的網(wǎng)頁(yè)漂浮廣告代碼
帶有關(guān)閉功能的漂浮圖片的實(shí)現(xiàn)方法有很多,下面為大家介紹下使用Javascript是如何實(shí)現(xiàn)的,喜歡的額朋友可以了解下2014-01-01
JavaScript異步回調(diào)的Promise模式封裝實(shí)例
這篇文章主要介紹了JavaScript異步回調(diào)的Promise模式封裝實(shí)例,本文通過分析easyjs的源碼得出,實(shí)例均參考easyjs,需要的朋友可以參考下2014-06-06
Three.Js實(shí)現(xiàn)顏色自定義變換效果實(shí)例
這篇文章主要給大家介紹了關(guān)于Three.Js實(shí)現(xiàn)顏色自定義變換效果的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02
bootstrap側(cè)邊欄圓點(diǎn)導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了bootstrap側(cè)邊欄圓點(diǎn)導(dǎo)航效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
JS基礎(chǔ)之undefined與null的區(qū)別分析
在JavaScript開發(fā)中,被人問到:null與undefined到底有啥區(qū)別?一時(shí)間不好回答,特別是undefined,因?yàn)檫@涉及到undefined的實(shí)現(xiàn)原理。2011-08-08
JS Thunk 函數(shù)的含義和用法實(shí)例總結(jié)
這篇文章主要介紹了JS Thunk 函數(shù)的含義和用法,結(jié)合實(shí)例形式總結(jié)分析了JS Thunk 函數(shù)的具體含義、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04
在線一元二次方程計(jì)算器實(shí)例(方程計(jì)算器在線計(jì)算)
在線一元二次方程式計(jì)算器實(shí)例分享,大家參考使用吧2013-12-12

