JS替換字符串中指定位置的字符(多種方法)
假設(shè)有一個(gè)字符串,可能'Good Morning'
也可能是'Hello World'
,我想將第五個(gè)字符,替換成'-'
。
因?yàn)樽址m然可以像數(shù)組那樣獲取某一位置字符'Hello World'[4]
,但是不能像數(shù)組那樣直接修改某一位置的字符'Hello World'[4] = '-'
,這樣是行不通的,但是可以把它切分成數(shù)組,修改某一位置的值,然后在合并回來。
方法1:
const replaceStr1 = (str, index, char) => { const strAry = str.split(''); strAry[index] = char; return strAry.join(''); } replaceStr(str1, 4, '-'); // => Good-Morning replaceStr(str2, 4, '-'); // => Hell- World
js的字符串有個(gè)substring
方法,用于提取字符串中介于兩個(gè)指定下標(biāo)之間的字符,也就是說可以用'Hello World'.substring(0, 4)
,得到Hell
,加上要替換的字符,再加上后面的字符串就可以。
方法2:
const replaceStr2 = (str, index, char) => { return str.substring(0, index) + char + str.substring(index + 1); } replaceStr2(str1, 4, '-'); // => Good-Morning replaceStr2(str2, 4, '-'); // => Hell- World
ps:下面看下js替換字符串中所有指定的字符
第一次發(fā)現(xiàn)JavaScript中replace()
方法如果直接用str.replace("-","!")
只會替換第一個(gè)匹配的字符.
而str.replace(/\-/g,"!")
則可以全部替換掉匹配的字符(g為全局標(biāo)志)。
replace()
Thereplace()methodreturnsthestringthatresultswhenyoureplacetextmatchingitsfirstargument
(aregularexpression)withthetextofthesecondargument(astring).
Iftheg(global)flagisnotsetintheregularexpressiondeclaration,thismethodreplacesonlythefirst
occurrenceofthepattern.Forexample,vars="Hello.Regexpsarefun.";s=s.replace(/\./,"!");//replacefirstperiodwithanexclamationpointalert(s);
producesthestring“Hello!Regexpsarefun.”Includingthegflagwillcausetheinterpreterto
performaglobalreplace,findingandreplacingeverymatchingsubstring.Forexample,vars="Hello.Regexpsarefun.";s=s.replace(/\./g,"!");//replaceallperiodswithexclamationpointsalert(s);
yieldsthisresult:“Hello!Regexpsarefun!”
所以可以用以下幾種方式.:
string.replace(/reallyDo/g,replaceWith); string.replace(newRegExp(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>
總結(jié)
到此這篇關(guān)于JS替換字符串中指定位置的字符的文章就介紹到這了,更多相關(guān)js替換字符內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript實(shí)現(xiàn)的石頭剪刀布游戲源碼分享
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的石頭剪刀布游戲源碼分享,挺好玩的小游戲,關(guān)鍵在一些算法上,需要的朋友可以參考下2014-08-08JS字符串分割方法整理匯總示例講解(3種截取方法和6個(gè)輔助方法)
JavaScript在開發(fā)中常常會需要截取字符串,而JS提供了slice()?、substring()、substr()?3種方法實(shí)現(xiàn)截取操作。另外還有字符串相關(guān)的6種輔助方法:indexOf()、lastIndexOf()、split()、join()、concat()、charAt()?。2023-02-02Electron 打包問題:electron-builder 下載各種依賴出錯(cuò)(推薦)
這篇文章主要介紹了Electron 打包問題:electron-builder 下載各種依賴出錯(cuò),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07關(guān)于javascript DOM事件模型的兩件事
DOM事件模型的兩件事:事件捕捉(Event Capture)的實(shí)現(xiàn)問題以及IE的高級事件處理模型的問題。2010-07-07在使用JSON格式處理數(shù)據(jù)時(shí)應(yīng)該注意的問題小結(jié)
這篇文章主要介紹了在使用JSON格式處理數(shù)據(jù)時(shí)應(yīng)該注意的問題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-05-05