JavaScript provides users with various methods and properties for string manipulation, to transform those strings or to search useful information from those strings. Sometimes we have various lines of code in which we need to make changes, search for a character or replace a character or remove a character from a string.

All these tasks become difficult to do and hence methods are provided by JavaScript that makes the job easier. Users can easily use these methods to manipulate a string and transform it. In this article we’ll discuss how to remove characters from strings in JavaScript, various ways and methods provided by JavaScript along with examples for your better understanding.

Remove Characters from Strings

JavaScript provides various in-built methods to remove characters from a string, some of which are listed below:

Using substring() method

The method substring() in JavaScript takes in two parameters, the starting and the ending indexes and returns a substring as an output. You can also pass only one parameter, the starting index and it’ll split the string from that index mentioned till the end of the string, here below is the example:

const example = “Welcome To the website!”

console.log(example.substring(3));

console.log(example.substring(3,4));

console.log(example.substring(3,9));

Output

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/1-6.jpg" data-lazy- height="216" src="data:image/svg xml,” width=”563″>

Using substr() method

Another method similar to substring() method is substr() that takes in two parameter starting and ending indexes and retrieves the characters between these specified indexes. Below is the example:

const example = ”  Welcome To the website!”

console.log(example.substr(2));

console.log(example.substr(1,3));

console.log(example.substr(1));

console.log(example.substr(2,example.length1));

Output

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/2-6.jpg" data-lazy- height="301" src="data:image/svg xml,” width=”556″>

Using replace() method

Another method in JavaScript is replace() method that takes in two parameters, the first one is the character to replace and the second one is the character to replace the character with. The output is a new string with the replaced values, below is an example:

const example = “Welcome To the website!”

console.log(example.replace(“the”, “our”));

console.log(example.replace(“W”,“w”));

console.log(example.replace(“e”,” “));

Output

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/3-6.jpg" data-lazy- height="220" src="data:image/svg xml,” width=”567″>

Using replace() method with Regular Expression

As we saw in the above replace() method example, when we wanted to remove the “e” character with whitespace, it only replaced the first occurring character. But what if we want to replace all the “e” characters or any character in the whole string with our desired character?

Well, for that we use Regular expression which provide us with various modifiers such a s global modifies “/g” that searches the whole string and replace all the particular mentioned character in the entire string, below is an example:

const example = “Welcome To the website!”

console.log(example.replace(“e”,” “));

console.log(example.replace(/e/g,” “));

Output

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/4-6.jpg" data-lazy- height="179" src="data:image/svg xml,” width=”564″>

Using slice() method

The slice() method works in a similar way as substring() and the substr() method, it takes in two parameters where we define the starting index and ending index of the part which we want to slice from the string. The output is the sliced string, below is an example:

const example = ”  Welcome To the website!”

console.log(example.slice(5, 9));

console.log(example.slice(1, 2));

console.log(example.slice(1, example.length1));

console.log(example.slice(2, example.length1));

Output:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/5-6.jpg" data-lazy- height="292" src="data:image/svg xml,” width=”634″>

Using split() method

Another method JavaScript provides for removing characters is split() method which is used along with join() method. Firstly we use split() method to remove our desired character and it returns an array of strings. After that join() method is used to join the string, demonstrated below by an example:

const example = ”  Welcome To the website!”

console.log(example.split(” “).join(“”));

console.log(example.split(“e”).join(“”));

Output:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/6-6.jpg" data-lazy- height="173" src="data:image/svg xml,” width=”632″>

Conclusion

Removing a specific character from a string can be difficult on your own sometimes, and hence methods are provided by JavaScript to manipulate string and remove characters from it. In this article we discussed various built-in methods in JavaScript for removing chcahters or part of a string from the whole string, along with examples. These methods are useful, and make the task of removing characters easy while dealing with lengthy codes.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/7011409B3A384F43A8417D1DAC68D179-150×150.jpg615e6f1b0b021.jpg" height="112" src="data:image/svg xml,” width=”112″>

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.