JavaScript

How to remove a character from a string using replace() method in JavaScript?

JavaScript is a programming language that is used to build web pages and web applications and make our web pages dynamic and interactive by giving them the ability to think and act. JavaScript offers us primitive data types which are predefined used to store data temporarily. One such datatype is a string that is textual content enclosed in double or single quotation marks. String datatype offers us different properties and inbuilt methods that we use to manipulate and play with the string. One such example is the replace() inbuilt method of the string.

What is the replace() method?

The replace() is an inbuilt method of JavaScript that replaces a character or string with another character or string in a specified string. First, it searches the character/string and then replaces it with the string/character we set. It should be noted that the replace() method does not change the original string and always returns a new string. The syntax of the replace() method is:

myString.replace(valueToBeReplaced, newvalue);

The JavaScript replace() method takes two arguments that are valueToBeReplaced and the newValue which is the value you want to replace it with. myString is the specified string where we are searching and replacing a value.

Removing a character

Now that we know what replace() method of JavaScript is, let us come to the main topic, which is how to remove a character from a string using the replace method. For this, suppose, you were writing a string and you mistakenly added a character in the middle and you want to remove that character, how will you do it? The answer is very simple that is we just have to provide an empty string in place of the second argument of the replace method(newValue):

var myStr = 'Hello y Nas';
console.log("original string:",myStr); // Hello y Nas
var newStr = myStr.replace('y', '');
console.log('After character removed: ', newStr); // Hello Nas

In this code, first, we initialized a string, and then console logged that string. Then we used the replace() method on the original string and removed the character y from it and the new string that is returned is stored in newStr. After that we console log the new string:

Now suppose you have added two-three characters by mistake hence we can also remove a string from a string using the replace method in the same manner:

var myStr = 'Hello wy Nas';
console.log("original string:",myStr); // Hello wy Nas
var newStr = myStr.replace('wy', '');
console.log('After string removed: ', newStr); // Hello Nas

Now that we know how to remove a character or a string from a specified string, let us use the replace() method with regular expressions. In the previous examples, the drawback of the replace method was that it only removed the first occurrence of the specified string or character in the original string. To overcome this drawback, we use the regular expression from which we can remove every instance of a specified character or string.

var myStr = 'I have two blue shirts and two blue caps';
console.log("original string:",myStr);
var newStr = myStr.replace(/blue/g, '');
console.log('After character removed: ', newStr);

We used the /blue/g which means it will remove every instance of blue in the myStr string:

Conclusion

The JavaScript replace() method is an inbuilt method that replaces a character or string in a specified string and returns the new string. The replace() method gives us the advantage of manipulating and playing with strings. One such advantage is that using the replace() method we can remove a character or string from an original string. We can also remove all the instances of a character or string in a specified string.

In this post, we first defined what replace() method is and then we went on to see how to remove a character from a string using the replace() method of JavaScript.

About the author

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.