Remove a Character From String in JavaScript (original) (raw)

Last Updated : 28 Apr, 2025

In JavaScript, a string is a group ofcharacters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like:

**Methods to Remove a Character From a String

Below are the following methods by which we can remove the character from a string:

**1. Using replace() - Removes First Occurrence

One of the most common methods to remove a character from a string is by using the replace() method. This method searches for a specified character or pattern in a string and replaces it with something else, which can be an empty string ("") to remove the character.

**Syntax

string.replace(searchValue, newValue);

**In the above syntax:

let s = "GeeksForGeeks"; res = s.replace("G", ""); console.log(res);

`

**2. Using replace() with a Regular Expression

This method removes all occurrences of a specified character or string from the input, unlike the previous one, which only removes the first occurrence. It uses a regular expression with the global flag to target and remove every instance.

**Syntax

string.replace(/pattern/g, '');

**In this syntax:

s = "GeeksForGeeks"; res = s.replace(/G/g, ""); console.log(res);

`

3. Using substring() Method

The substring() method works similarly to slice(), but it does not support negative indices. It extracts a portion of a string between two specified indices.

**Syntax

string.substring(startIndex, endIndex);

**In the above syntax

let str = "Hello World"; let newStr = str.substring(0, 4) + str.substring(5); console.log(newStr);

`

4. Using slice() Method

The slice() method is similar to substring(), but it allows using negative indices to refer to positions from the end of the string. It’s useful for more flexible string manipulation.

**Syntax

string.slice(startIndex, endIndex);

**In the above syntax

let str = "Hello World"; let newStr = str.slice(0, 4) + str.slice(5); console.log(newStr);

`

**Output

Hell Wrld

5. Using split() and join()

The split() method divides a string into an array of substrings, and the join() method joins an array back into a string. This approach uses the split() and join() methods.

**Syntax

string.split(separator).join(newString);

**In the above syntax

let s = "GeeksForGeeks"; let res = s.split("G").join(""); console.log(res);

`

6. Using Array.filter()

This method converts the string into an array of characters, use the filter() method to remove specific characters, and then join the array back into a string.

JavaScript `

let s = "GeeksForGeeks"; let c = 'G'; let res = Array.from(s) .filter(char => char !== c) .join(''); console.log(res);

`

Conclusion

**Removing characters from a **string in JavaScript can be done in several ways, depending on the situation. Whether using **replace(), split() and join(), **substring(), slice(), or advanced methods like **filter(), each approach is suited to specific needs.