We can use JavaScript to remove a character from a string easily by using the JavaScript String replace() method.

someString.replace("s", "");

Let’s see this code in action below.

var someString = "This is a short sentence."

someString = someString.replace("s", "");

console.log(someString);

#Output:
Thi 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 characters from a string variable.

We can easily remove character(s) from a string in JavaScript.

The easiest way to remove a character 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 a character, we pass any character (“s”) 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 character. If we want to remove all instances of the character 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 a Character From a String

Let’s say we wanted to remove all instances of a character instead of just the first occurrence of that character. 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(/s/g, "");

Using the regular expression /s/g makes it so we replace ALL instances of the character s 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(/s/g, "");

console.log(someString);

#Output:
Working with Python, Javacript, PHP, HTML, SAS, and other programming language can allow u to create amazing program which 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 characters were not removed. If we want all s characters removed regardless of the case, we would use the following code:

someString.replace(/s/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(/s/gi, "");

console.log(someString);

#Output:
Working with Python, Javacript, PHP, HTML, A, and other programming language can allow u to create amazing program which make our work more efficient, repeatable, and accurate.

Hopefully this article has been useful for you to learn how to use JavaScript to remove a character from a string.

Categorized in:

JavaScript,

Last Update: April 2, 2024