HOWTO · JavaScript
How to Remove a Character From a String in JavaScript
It demonstrates different ways to remove a character from a string in JavaScript
On this page
JavaScript strings are immutable, so removing a character creates and returns a new string; the original value stays unchanged. Choose the operation by what identifies the character: use a global regular expression to remove every match, slice() to remove one character at a position, or replace() with a string to remove only the first match. The examples use DelftStack, where the letter t appears twice, so the difference between these choices is visible.
Remove every matching character with a global regular expression
Pass a regular expression with the g flag to replace() when every occurrence should be removed. Without g, a regular expression replaces only its first match. The replacement string '' contributes no characters, while the result is assigned to a new variable because replace() does not modify the original string.
To remove every matching t, run this example:
const original = "DelftStack";
const result = original.replace(/t/g, "");
console.log(result);
DelfSack
This literal pattern is appropriate when the character is known in advance. If a pattern is built from user-provided text, escape regular-expression metacharacters before constructing the RegExp; otherwise characters such as . or * act as operators instead of matching literally. For a fixed character, a regular expression character class can also express the match, but escape characters that have special meaning inside that class.
Remove one character at a known index with slice()
JavaScript string indices start at zero. In DelftStack, t is at index 4, so take the portion before that index and join it to the portion after the character. slice() returns each part without changing the source string. This is positional removal: at a valid index, it removes the character at that position whether or not it matches a character you searched for. Check text[index] first when the match matters.
To remove the character at index 4, run this example:
const original = "DelftStack";
const index = 4;
const result = original.slice(0, index) + original.slice(index + 1);
console.log(result);
DelfStack
For a reusable function, check that the index is an integer from 0 through text.length - 1 before joining the slices. An index equal to or greater than the string length leaves the string unchanged with this expression. A negative index has different slice() behavior and can remove text from the end, so validate it rather than treating every out-of-range value as a no-op. JavaScript string indexing operates on UTF-16 code units; a visible symbol represented by a surrogate pair may occupy two indices.
Remove only the first matching character with replace()
When the search value is a string, replace() removes only its first matching occurrence. This is the shortest choice when the first match is the intended one. Compare its result with the global-regex example: the first t disappears and the later t remains. Use a regular expression with g instead when all matching occurrences should be removed.
To remove only the first matching t, run this example:
const original = "DelftStack";
const result = original.replace("t", "");
console.log(result);
DelfStack
These methods return strings, so you can store a result, pass it to another function, or reassign a variable declared with let. They do not change a string held in a const binding; const prevents rebinding the variable, while the string value itself is immutable. For repeated transformations, choose the method whose matching rule is clearest, then use its returned value as the input to the next operation. Search-based methods such as replace() leave the text unchanged when the search value is absent; index-based removal follows the position and removes the character there when the index is valid.