Replace Multiple Characters In JavaScript

To replace multiple characters in JavaScript, you can use any of the following methods –

  1. replace() method with a regular expression matching the characters to be replaced as the first argument, and the replacement string as the second argument.
  2. Using the replaceAll() Method
  3. Using split() and join() Methods
  4. Using for loop
  5. Using Array Methods

Let’s see each of these methods in detail below.

replace multiple characters in javascript

Replace Multiple Characters In JavaScript Using replace()

var str = "Hello _world!";

The replace() method can be used to replace multiple characters in JavaScript. We just need to pass a regular expression as the first argument, and a replacement string as the second argument.

For example, if we have the following string –

And we want to replace all occurrences of _, space and ! with *, we can use the replace() method as follows –

var newString = str.replace(/_| |!/g, "*");

console.log(newString);

Note that we have used a regular expression (/_| |!/g) to match all occurrences of _ or space or ! in the string. The g at the end of the regular expression denotes a global search for all occurrences of the pattern in the string.

The above code will return the following output –

“Hello**world*”

Replace Multiple Characters In JavaScript Using replaceAll()

The replaceAll() method is a little bit different from the replace() method, as it takes two arguments –

  • A String to be replaced
  • A replacement

This method can also be used to replace multiple characters in JavaScript.

For example, if we have the following string –

var str = "Hello _world!";

And we want to replace all occurrences of _, space and ! with *, we can use the replaceAll() method as follows –

var newStr = str.replaceAll("_", "*").replaceAll(" ", "*").replaceAll("!", "*");

console.log(newStr);

The above code will return the following output –

“Hello**world*”

As you can see, we have chained the replaceAll() method to replace all occurrences of _, space and ! with *.

Replace Multiple Characters In JavaScript Using split() And join() Methods

Another way to replace multiple characters in JavaScript is to use the split() and join() methods. The split() method splits a string into an array of substrings, and the join() method joins all elements of an array into a string.

For example, if we have the following string –

var str = "Hello _world!";

And we want to replace all occurrences of _, space and ! with *, we can use the split() and join() methods as follows –

var newStr = str.split("_").join("*").split(" ").join("*").split("!").join("*");

console.log(newStr);

The above code will return the following output –

“Hello**world*”

As you can see, we have first split the string by _ and then joined it back with *. We have done the same for space and !.

Replace Multiple Characters In JavaScript Using for Loop

Another way to replace multiple characters in JavaScript is to use a for loop. This method is not as efficient as the other methods, but it is still worth mentioning.

For example, if we have the following string –

var str = "Hello _world!";

And we want to replace all occurrences of _, space and ! with *, we can use a for loop as follows –

for(var i=0;i<str.length;i++){

if(str.charAt(i)=="_" || str.charAt(i)==" " || str.charAt(i)=="!"){

str = str.substr(0, i) + "*" + str.substr(i+1);

}

}

console.log(str);

The above code will return the following output –

“Hello**world*”

As you can see, we have looped through each character in the string and replaced all occurrences of _, space and ! with *.

Replace Multiple Characters In JavaScript Using Array Methods

Another way to replace multiple characters in JavaScript is to use array methods like map() or filter(). This method is not as efficient as the other methods, but it is still worth mentioning.

For example, if we have the following string –

var str = "Hello _world!";

And we want to replace all occurrences of _, space and ! with *, we can use array methods as follows –

var newStr = Array.prototype.map.call(str, function(x){

if(x=="_" || x==" " || x=="!"){return "*";} else {return x;}

}).join("");

console.log(newStr);

The above code will return the following output –

“Hello**world*”

As you can see, we have used the map() method to loop through each character in the string and replace all occurrences of _, space and ! with *. We have then joined all the characters back into a string.

Replace Multiple Characters In JavaScript Summary

In this article, we looked at how to replace multiple characters in JavaScript using different methods. The replace() method is the most efficient way to replace multiple characters in JavaScript. The other methods are also quite efficient, but they are not as widely used as the replace() method.

Leave a Reply