Replace all occurrences of a character in JavaScript
This post will discuss how to replace all occurrences of a character in a string in JavaScript.
There are several ways to replace all occurrences of a character in a string in JavaScript. Here are some of the possible functions:
1. Using replace() function
The replace() function replaces the matched values in a string with a new value. To use it, we need to pass a regular expression that matches the character we want to replace and a new value that is the replacement. To replace all instances of a character in a string, we can use it with a regular expression that has the global flag (g). For example, this code replaces all occurrences of "i" with "x" in the string "Mississippi".
|
1 2 3 4 5 |
let str = "Mississippi"; let newStr = str.replace(/i/g, "x"); console.log(newStr); // "Mxssxssxppx" |
The regular expression /i/g matches the character "i" globally (g), meaning it will find all occurrences in the string. The replacement value is the character "x", which will replace each matched character.
2. Using replaceAll() function
This function replaces all occurrences of a string with another string. To use it, we need to pass the string we want to replace and the replacement string as arguments. This function is part of the ECMAScript 2021 specification and may not be supported by all browsers. For example, the following snippet replaces all occurrences of "i" with "x" in the string "Mississippi".
|
1 2 3 4 5 |
let str = "Mississippi"; let newStr = str.replaceAll("i", "x"); console.log(newStr); // "Mxssxssxppx" |
3. Using split() and join() functions
We can use the combination of split() and join() functions to split a string into an array of substrings and join them back into a new string. To use them, we need to first split the string by the character we want to replace using the split() function, then join the array elements with the replacement character using the join() function. For instance:
|
1 2 3 4 5 |
let str = "Mississippi"; let newStr = str.split("i").join("x"); console.log(newStr); // "Mxssxssxppx" |
The split() function will return an array of substrings that are separated by the character "i", such as ['M', 'ss', 'ss', 'pp', '']. The join() function will return a string that is composed of the array elements joined by the character "x".
4. Using a for loop
We can use a for loop to iterate over the characters of the string, and use a conditional statement to check if the current character is the one we want to replace. If so, we can concatenate the new character to a new string variable. Otherwise, we can concatenate the original character. For example, this code replaces all occurrences of "i" with "x" in the string "Mississippi".
|
1 2 3 4 5 6 7 8 9 10 11 |
let str = "Mississippi"; let newStr = ""; for (let i = 0; i < str.length; i++) { if (str[i] === "i") { newStr += "x"; } else { newStr += str[i]; } } console.log(newStr); // "Mxssxssxppx" |
That’s all about replacing all occurrences of a character in a string in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)