Replace all matches in JavaScript and remove text from a string using literal searches or regular expressions.

JavaScript replace global: First Match or Every Match?

Call replace() on a JavaScript string to substitute its first matching fragment. To replace matches globally in JavaScript, use a regular expression with the g flag or call replaceAll() for every occurrence of literal text. Both methods return a new string, so keep the result.

string.replace JavaScript calls and the first match

Pass the search value first and the replacement second. When the search value is a string, replace() finds only its first occurrence:

const original = "red, red, blue";
const updated = original.replace("red", "green");

console.log(updated);  // "green, red, blue"
console.log(original); // "red, red, blue"

The string.replace JavaScript form in this example treats "red" as literal text. It is case-sensitive: "Red" would not match "red". The original value remains unchanged because strings are immutable. Assign the returned value to a variable, or reassign it when the variable was declared with let. If there is no match, the result has the same content as the original.

This distinction matters when updating a value used later. Calling original.replace("red", "green") without using the return value computes a result and discards it. Logging original afterward will still show the initial text.

Replace every occurrence

For literal text, replaceAll() makes the intent clear:

const colors = "red, red, blue";
const updated = colors.replaceAll("red", "green");
console.log(updated); // "green, green, blue"

For a JavaScript replace global operation with a pattern, pass a regular expression with the g flag to replace():

const colors = "red, Red, blue";
const updated = colors.replace(/red/gi, "green");
console.log(updated); // "green, green, blue"

Here g selects all matches and i makes the search case-insensitive. Without g, the regular expression replaces only its first match. replaceAll() also accepts a regular expression, but it throws a TypeError if that expression lacks the g flag. See the MDN replace reference for the method’s full behavior.

A global replacement in JavaScript is useful when every instance should change. Check the expected input before applying it: replacing "cat" also changes that sequence inside "catalog". If you mean whole words, a pattern such as /\bcat\b/g expresses a different rule. Test boundary behavior against the actual characters in your data.

Remove a fragment

A JavaScript string remove operation uses an empty replacement. The same first-versus-all distinction applies:

const text = "draft-draft-final";
console.log(text.replace("draft-", ""));    // "draft-final"
console.log(text.replaceAll("draft-", "")); // "final"

A regular expression can remove a class of characters as well. For example, "A1 B2".replace(/\d/g, "") returns "A B". The g flag matters here: without it, the second digit remains.

When you replace text in a JavaScript string, check the output against cases with no match, one match, and several matches. Include mixed case if input is not normalized. These small examples expose whether the search is literal, whether case matters, and whether the result contains the intended number of changes.

To replace all matching text globally in JavaScript, also consider the shape of the input. Removing separators from a structured identifier may combine fields that must remain distinct. Keep the original available when later code still needs the unmodified value.

Choose the search and replacement

Use a literal search string when the characters themselves are the target, especially when the search text comes from a user. Building a regular expression from that text can give characters such as . or * special meaning. Choose a regular expression when the match needs a pattern, a boundary, or flags such as i.

Replacement strings also interpret special sequences such as $&, which inserts the matched text. If replacement text must be inserted literally, return it from a function: text.replaceAll(search, () => replacement). Choose replace() for one literal match, replaceAll() for all literal matches, and a regular expression when the selection itself needs a pattern.