js替換字符串中所有指定的字符(實(shí)現(xiàn)代碼)
第一次發(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:用于替換的子字符串。
Js代碼
<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>
以上這篇js替換字符串中所有指定的字符(實(shí)現(xiàn)代碼)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JS+CSS實(shí)現(xiàn)TreeMenu二級(jí)樹(shù)形菜單完整實(shí)例
這篇文章主要介紹了JS+CSS實(shí)現(xiàn)TreeMenu二級(jí)樹(shù)形菜單,以完整實(shí)例形式較為詳細(xì)的分析了JS二級(jí)樹(shù)形菜單的節(jié)點(diǎn)元素操作技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-09-09
H5如何實(shí)現(xiàn)喚起APP及調(diào)試bug解決
這篇文章主要為大家介紹了H5如何實(shí)現(xiàn)喚起APP及調(diào)試bug解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
不錯(cuò)的JS中變量相關(guān)的細(xì)節(jié)分析
不錯(cuò)的JS中變量相關(guān)的細(xì)節(jié)分析...2007-08-08
基于Typescript與Axios的接口請(qǐng)求管理詳解
接口是一系列抽象方法的聲明,是一些方法特征的集合,這些方法都應(yīng)該是抽象的,需要由具體的類(lèi)去實(shí)現(xiàn),然后第三方就可以通過(guò)這組抽象方法調(diào)用,讓具體的類(lèi)執(zhí)行具體的方法,這篇文章主要給大家介紹了基于Typescript與Axios的接口請(qǐng)求管理的相關(guān)資料,需要的朋友可以參考下2021-09-09
JavaScript實(shí)現(xiàn)的字符串replaceAll函數(shù)代碼分享
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的字符串replaceAll函數(shù)代碼分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04
JS實(shí)現(xiàn)電腦虛擬鍵盤(pán)打字測(cè)試
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)電腦虛擬鍵盤(pán)打字測(cè)試,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
JS判斷字符串是否為整數(shù)的方法--簡(jiǎn)單的正則判斷
今天小編就為大家分享一篇JS判斷字符串是否為整數(shù)的方法--簡(jiǎn)單的正則判斷,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
基于JavaScript實(shí)現(xiàn)一個(gè)月餅音樂(lè)播放器
這篇文章主要為大家詳細(xì)介紹了如何利用JavaScript實(shí)現(xiàn)一個(gè)簡(jiǎn)單的月餅音樂(lè)播放器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-09-09
用JavaScript和jQuery實(shí)現(xiàn)瀑布流
本篇文章主要介紹了用JavaScript和jQuery實(shí)現(xiàn)瀑布流的方法,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
webpack cjs運(yùn)行時(shí)分析示例詳解
這篇文章主要介紹了webpack cjs運(yùn)行時(shí)分析,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12

