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:
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):
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:
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.
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.