This article talks about how to replace all occurrences of a character in a javascript string using different methods and illustration through examples.
Table of Contents:-
- Javascript replace all occurrences of a character in string using replace()
- Javascript replace all occurrences of a character in string using replaceAll()
- Javascript replace all occurrences of a character in string using split and join
- Javascript replace all occurrences of a character in string using substring and indexOf
Javascript replace all occurrences of a character in string using replace()
The replace() method in javascript looks for a pattern and replaces some or all of its occurrences with a replacement(character/string). The pattern can be a character or a string or regExp.
Syntax:-
replace(regexp, newCharacter) replace(characterToBeReplaced, newCharacter)
Example:-
Frequently Asked:
Replace all occurrences of “x” with “”
let dummyString = "This is x a x language, This is x the most popxular xlanguage." newString = dummyString.replace(/x/gi, "") console.log(newString)
Output:-
This is a language, This is the most popular language.
Javascript replace all occurrences of a character in string using replaceAll()
The replaceAll() method in javascript returns a new string with all matches of a pattern replaced by a replacement(character/string).
Syntax:-
replaceAll(regexp, newCharacter) replaceAll(characterToBeReplaced, newCharacter)
Example:-
Replace all occurrences of “x” with “”
let dummyString = "This is x a x language, This is x the most popxular xlanguage." newString = dummyString.replaceAll("x", "") console.log("Original string is: " + dummyString) console.log("Replaced string is: " + newString)
Output:-
Original string is: This is x a x language, This is x the most popxular xlanguage. Replaced string is: This is a language, This is the most popular language.
Replace all occurrences of a character in string using split and join
This section will replace all occurrences of a character in javascript by splitting and joining an array. The solution is to split the original string by the search character and then replace it with a new character. Then, finally, joining back into one string.
Syntax:-
newString = originalString.split(characterToBeReplaced).join(newCharacter)
Example:-
Replace all occurrences of “x” with “”
let dummyString = "This is x a x language, This is x the most popxular xlanguage." newString = dummyString.replaceAll("x", "") newString = dummyString.split('x').join('') console.log("Original string is: " + dummyString) console.log("Replaced string is: " + newString)
Output:-
Original string is: This is x a x language, This is x the most popxular xlanguage. Replaced string is: This is a language, This is the most popular language.
Javascript replace all occurrences of a character in string using substring and indexOf
In this section, we will replace all occurrences of a character in javascript by:
- Finding the indexOf of a pattern.
- Identifying the character/string to be replaced based on the index and then replacing it with the new character/space/string.
- Finally, concatenating the substrings to one string.
Example:-
Replace all occurrences of “x” with “”
let dummyString = "This is x a x language, This is x the most popxular xlanguage." let newString = "" let i=-1; let findToken = 'x' let newToken ='' while (( i=dummyString.indexOf(findToken,i>=0? i= newToken.length : 0 )) !== -1) { dummyString= dummyString.substring(0,i) + newToken + dummyString.substring(i+findToken.length) } newString = dummyString console.log(dummyString)
Output:-
This is a language, This is the most popular language.
Read More:
- Javascript: Replace all occurrences of string (4 ways)
- Javascript check if string contains substring
- Javascript: Check if string is empty (6 ways)
We hope this article helped you to replace all occurrences of a character in a javascript string. Good Luck !!!