JS中實(shí)現(xiàn)replaceAll的方法(實(shí)例代碼)
我們在Java中可以使用replaceAll()方法對字符串進(jìn)行批量替換,但在JS中replaceAll()方法是undefined,JS中只存在replace()方法,因此我們可以自己封裝JS中replaceAll()方法供我們便捷使用。
一、使用replace()方法進(jìn)行替換
定義一個字符串:
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); }
定義一個字符串:
var str = "hello world";
使用replaceAll()方法進(jìn)行批量替換:
console.log(str.replaceAll("l", "i"));
輸出:
“heiio worid”
只需要執(zhí)行一次,就完成了全部替換需求。
補(bǔ)充
第一次發(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!”
所以可以用以下幾種方式.:
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>?
到此這篇關(guān)于JS中實(shí)現(xiàn)replaceAll的方法(實(shí)例代碼)的文章就介紹到這了,更多相關(guān)JS replaceAll方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- javascript中使用replaceAll()函數(shù)實(shí)現(xiàn)字符替換的方法
- Javascript中正則表達(dá)式的全局匹配模式分析
- Javascript中使用exec進(jìn)行正則表達(dá)式全局匹配時的注意事項(xiàng)
- JavaScript實(shí)現(xiàn)的字符串replaceAll函數(shù)代碼分享
- javascript實(shí)現(xiàn)全局匹配并替換的方法
- java中replaceAll替換圓括號實(shí)例代碼
- Java中replace與replaceAll的區(qū)別與測試
- java字符串的替換replace、replaceAll、replaceFirst的區(qū)別說明
- Java replaceAll()方法報錯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 replace 與replaceall實(shí)例用法詳解
- Flex 字符串ReplaceAll使用說明
- JavaScript中使用replace結(jié)合正則實(shí)現(xiàn)replaceAll的效果
相關(guān)文章
JavaScript判斷是否為數(shù)字的多種方法小結(jié)
這篇文章主要介紹了JavaScript判斷是否為數(shù)字的多種方法小結(jié),本文給大家分享三種方法,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01淺談addEventListener和attachEvent的區(qū)別
下面小編就為大家?guī)硪黄獪\談addEventListener和attachEvent的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07javascript中的緩動效果實(shí)現(xiàn)程序
javascript中的緩動效果可以應(yīng)用于很多地方,比如距離位移上的變化:圖片的滾動、焦點(diǎn)圖的輪轉(zhuǎn)切換,透明度上的變化:漸隱漸現(xiàn)。凡是存在運(yùn)動的狀態(tài)都適用,下面以最基本的塊在容器內(nèi)從左到右滑動為例,講下幾種不同的緩動處理方式2012-12-12JS實(shí)現(xiàn)從連接中獲取youtube的key實(shí)例
這篇文章主要介紹了JS實(shí)現(xiàn)從連接中獲取youtube的key的方法,涉及javascript字符串操作的相關(guān)技巧,需要的朋友可以參考下2015-07-07