JavaScript regex replace uses String.replace() to find a pattern and return a new string with replacement text. The original string remains unchanged. Use a plain search string for a literal match, or a regular expression when you need flags, groups, or pattern rules.
For example, “red red”.replace(/red/, “blue”) returns “blue red”. The regex matches the first occurrence only because it does not include the global flag.
JavaScript regex replace: Replace one match and return a new string
A string search finds the exact sequence you provide: “cat”.replace(“cat”, “dog”) returns “dog”. A regex can express a broader rule, such as /cat/ or /cat[0-9]/.
The replacement argument can be a fixed string. In “Order 42”.replace(/[0-9]+/, “complete”), the digits are replaced, producing “Order complete”. Without g, a regex replacement stops after the first match. This behavior applies even when several matches exist.
Because replace() returns a string, assign its result when you need to keep it: const updated = source.replace(/old/, “new”). Calling the method does not modify source.
Regex replacement in JavaScript: Use g and i to replace all matches
The g flag means global matching. It makes the replacement continue through every match instead of stopping at the first one. For example, “Error error ERROR”.replace(/error/gi, “notice”) returns “notice notice notice”.
The i flag makes matching case-insensitive. It allows /error/i to match error, Error, and ERROR. Combine flags when needed: /error/gi replaces all case variations.
- /word/ replaces the first matching occurrence.
- /word/g replaces every matching occurrence with matching case.
- /word/i replaces the first case-insensitive occurrence.
- /word/gi replaces every case-insensitive occurrence.
A global regex does not change the original string, and the returned value is still a new string. Use a replacement string when every match should receive the same output.
How do you replace text in JavaScript with captured groups?
Capturing groups preserve parts of the match so the replacement can reuse them. In “Smith, Ada”.replace(/(\w+),\s*(\w+)/, “$2 $1”), group one captures Smith and group two captures Ada. The result is “Ada Smith”.
Replacement references begin with a dollar sign. $1 inserts the first captured group, $2 inserts the second, and $& inserts the complete match. Add g when the same grouped structure can occur more than once: text.replace(/(\w+),\s*(\w+)/g, “$2 $1”).
Use noncapturing parentheses, (?:…), for grouping that should not create a replacement reference. This keeps group numbers stable when the pattern becomes more complex.
How do callbacks compute replacements and escape literal input?
Pass a function instead of a replacement string when the output depends on the match. The callback receives the complete match first, followed by captured groups, the match position, and the complete input string.
For example, “item-7 item-12”.replace(/item-(\d+)/g, (match, number) => “product ” + Number(number)) returns “product 7 product 12”. The callback can format values, perform calculations, or choose different output for each match.
A regex pattern is different from escaped literal text. If user input is “a+b”, putting it directly into new RegExp(userInput, “g”) treats + as a regex operator rather than a literal character. For literal matching, use a string search or escape regex metacharacters first:
const escaped = userInput.replace(/[.*+?^${}()|[\]\\]/g, “\\$&”);
Then create the pattern with new RegExp(escaped, “g”). This preserves the input as literal text while still allowing global replacement.









