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

Javascript
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

Similar Reads

Method 1: Using JavaScript replace() Method

The replace() method replaces the first occurrence of a specified character/string with another character/string....

Method 2: Using JavaScript replace() Method with a Regular Expression

This method is used to remove all occurrences of a specified character or string from the input string, unlike the previous method, which removes only the first occurrence....

Method 3: Using JavaScript slice() Method

The slice() method is used to extract parts of a string between specified indices....

Method 4: Using JavaScript substr() method

The substr() method is used to remove a character from a specific index within the string....

Method 5: Using JavaScript split() and join() methods

The split() method is used to split a string into an array of substrings based on a specified separator. By splitting the string at the character to be removed, and then joining the array elements back into a string, the desired character can be effectively removed....

Method 6: Using JavaScript splice() Method

The splice() method changes the contents of a string by removing or replacing existing elements and/or adding new elements. We can use this method to remove a character at a specified index from the string....