JS中實(shí)現(xiàn)replaceAll的方法(實(shí)例代碼)
我們?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í)行一次,就完成了全部替換需求。
補(bǔ)充
第一次發(fā)現(xiàn)JavaScript中replace() 方法如果直接用str.replace("-","!") 只會(huì)替換第一個(gè)匹配的字符.
而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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- javascript中使用replaceAll()函數(shù)實(shí)現(xiàn)字符替換的方法
- 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ū)別說明
- 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 replace 與replaceall實(shí)例用法詳解
- Flex 字符串ReplaceAll使用說明
- JavaScript中使用replace結(jié)合正則實(shí)現(xiàn)replaceAll的效果
相關(guān)文章
javascript實(shí)現(xiàn)日歷控件(年月日關(guān)閉按鈕)
經(jīng)常使用google的朋友一定對(duì)google絢麗的日歷控件記憶猶新吧,那我們也來實(shí)現(xiàn)一個(gè),雖然功能和效果比不上,但重要的是實(shí)現(xiàn)的過程2012-12-12
在 webpack 中使用 ECharts的實(shí)例詳解
這篇文章主要介紹了在 webpack 中使用 ECharts的實(shí)例代碼,需要的朋友可以參考下2018-02-02
淺談Javascript中Object與Function對(duì)象
JavaScript的面向?qū)ο笫腔谠蔚模袑?duì)象都有一條屬于自己的原型鏈。Object與Function可能很多看Object instanceof Function , Function instanceof Object都為true而迷惑,所以首先看下對(duì)象的實(shí)例2015-09-09
js實(shí)現(xiàn)可拖動(dòng)DIV的方法
這篇文章主要介紹了js實(shí)現(xiàn)可拖動(dòng)DIV的方法,有需要的朋友可以參考一下2013-12-12
前端實(shí)現(xiàn)文件下載的幾種常用方式總結(jié)
這篇文章主要給大家介紹了關(guān)于前端實(shí)現(xiàn)文件下載的兩種常用方式,兩種方法均通過創(chuàng)建臨時(shí)URL并觸發(fā)下載實(shí)現(xiàn),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-11-11
javascript 對(duì)象屬性property與元素屬性attribute的瀏覽器支持
對(duì)象屬性property與元素屬性attribute的瀏覽器支持情況,大家可以參考下。2010-10-10
bootstrap-table實(shí)現(xiàn)服務(wù)器分頁(yè)的示例 (spring 后臺(tái))
本篇文章主要介紹了bootstrap-table實(shí)現(xiàn)服務(wù)器分頁(yè)的示例 (spring 后臺(tái)),具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09

