To remove multiple characters from a string in JavaScript, the easiest way to do this is to use the JavaScript String replace() method.

someString.replace(/[sh]/, "");

Where the characters s and h in the code above can be any characters and as many characters as you want.

Let’s see this code in action below.

var someString = "This is a short sentence."

someString = someString.replace(/[sh]/, "");

console.log(someString);

#Output:
Tis is a short sentence.

When using string variables in JavaScript, we can easily perform string manipulation to change the value of the string variables.

One such manipulation is to remove multiple characters from a string variable.

We can easily remove multiple characters from a string in JavaScript.

The easiest way to remove multiple characters from a string using JavaScript is with the JavaScript String replace() method.

The replace() method takes two arguments: the substring we want to replace, and the replacement substring. In this case, to remove multiple characters, we pass any characters we want (/[abc]/) as the first argument, and an empty string (“”) as the second argument.


As you can see, our JavaScript code only removed the first instance of the characters. If we want to remove all instances of the characters from the string, we will need to add to our code above.

Let’s take a look at how to do this below.

Using JavaScript to Remove All Instances of Multiple Characters From a String

Let’s say we wanted to remove all instances of multiple characters instead of just the first occurrence of those characters. We would have to change our code above slightly.

We simply need to update the regex expression in our replace method. Here is the code:

someString.replace(/[sh]/g, "");

Using the regular expression /sh/g makes it so we replace ALL instances of the characters s and h in the string.

Let’s see this in an example below. We will use some example text from our About page.

var someString = "Working with Python, Javascript, PHP, HTML, SAS, and other programming languages can allow us to create amazing programs which make our work more efficient, repeatable, and accurate."

someString = someString.replace(/[sh]/g, "");

console.log(someString);

#Output:
Working wit Pyton, Javacript, PHP, HTML, SAS, and oter programming language can allow u to create amazing program wic make our work more efficient, repeatable, and accurate.

Notice in our example above that our code is case sensitive, which is why all capital S and H characters were not removed. If we want all s and h characters removed regardless of the case, we would use the following code:

someString.replace(/[sh]/gi, "");

And here would be the output of the same example:

var someString = "Working with Python, Javascript, PHP, HTML, SAS, and other programming languages can allow us to create amazing programs which make our work more efficient, repeatable, and accurate."

someString = someString.replace(/[sh]/gi, "");

console.log(someString);

#Output:
Working wit Pyton, Javacript, PP, TML, A, and oter programming language can allow u to create amazing program wic make our work more efficient, repeatable, and accurate.

Hopefully this article has been useful for you to learn how to remove multiple characters from a string in JavaScript.

Categorized in:

JavaScript,

Last Update: April 2, 2024