How do I replace all occurrences of a string in JavaScript?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In JavaScript, replacing every occurrence of a substring is simple once you choose the right tool. For most modern environments, replaceAll is the clearest option for plain string-to-string replacement. If you need pattern matching, case-insensitive replacement, or older compatibility, use replace with a global regular expression.
The Modern Plain-String Answer: replaceAll
If you want to replace every literal occurrence of a string, use replaceAll:
Output:
This is the most readable solution when the search value is a plain string, not a regex pattern.
When To Use replace With a Regular Expression
Use replace when you need pattern-based matching, such as case-insensitive replacement:
The flags matter:
- '
gmeans global, so all matches are replaced' - '
imakes the match case-insensitive'
Without g, only the first match is replaced.
Dynamic Search Strings Need Care
If the text to find comes from a variable and you still want regex behavior, special characters must be escaped. Otherwise characters like . or * change the meaning of the pattern.
For plain literal replacement, replaceAll is safer because it does not treat the search string as a regex:
Output:
With regex, . means "any character," so using it casually can produce very different results.
Older Fallback: split and join
If you are working in an older environment without replaceAll, a plain-string fallback is:
This works for straightforward cases, though it is less expressive than replaceAll or regex-based replace.
When the Replacement Depends on the Match
If you need dynamic replacement logic, replace can take a callback:
That is useful when the replacement depends on capture groups or some computed rule, which replaceAll does not handle as directly for regex-style matching.
A Small Utility Function
If you want a helper that handles the two most common cases, keep it explicit:
That function uses replaceAll for literal exact matches and falls back to a safely escaped regex when case-insensitive behavior is required.
Which Method Should You Prefer
Use this rule of thumb:
- use
replaceAllfor literal substring replacement - use
replacewith regex for patterns or flags - use
split().join()only as a compatibility fallback
That keeps the code readable and avoids accidental regex bugs.
Common Pitfalls
The most common mistake is writing text.replace("x", "y") and expecting every match to change. Without a global regex or replaceAll, only the first occurrence is replaced.
Another mistake is building a regex from user input without escaping it first. A search string like a.b should usually mean the literal characters a.b, not "a followed by any character followed by b."
A third issue is forgetting browser or runtime compatibility when using replaceAll in older projects.
Summary
- '
replaceAllis the clearest way to replace every literal occurrence of a string.' - '
replacewith a global regex is the right tool when you need patterns or flags.' - Escape dynamic input before building a regex from it.
- '
split().join()works as a plain-string fallback for older environments.' - Choose literal replacement versus pattern replacement deliberately.
Related reading
- How to access the index value in a 'for' loop?
- How to disable text selection highlighting
- Is there an "exists" function for jQuery?
- JavaScript closure inside loops – simple practical example
- Loop (for each) over an array in JavaScript
- Loop through an array in JavaScript
- Set cellpadding and cellspacing in CSS?
- What does the !! operator do in JavaScript?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.