How to use replace() Method In Javascript
The replace() method replaces the first occurrence of a specified character/string with another character/string.
Syntax:
string.replace('characterToReplace', '');
Example: This example shows the above-explained approach
function removeCharacter() {
let originalString = "w3wiki";
newString = originalString.replace("G", "");
console.log(newString);
}
removeCharacter();
Output
eeksForGeeks
Remove a Character From String in JavaScript
We are given a string and the task is to remove a character from the given string. We have many methods to remove a character from a string that is described below.
Table of Content
- Method 1: Using JavaScript replace() Method
- Method 2: Using JavaScript replace() Method with a Regular Expression
- Method 3: Using JavaScript slice() Method
- Method 4: Using JavaScript substr() method
- Method 5: Using JavaScript split() and join() methods
- Method 6: Using JavaScript splice() Method