String.prototype.replace(pattern, replacement) returns a new string with matches swapped out. A plain string pattern replaces only the first match. A RegExp with g can replace every match.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call string.replace. Start with this line.
JavaScript
str.replace(pattern, replacement)
Call
What it does
str.replace(pattern, replacement)
Primary signature.
string.replace(...)
Call with the arguments your case needs.
Built into JavaScript. Call replace(...) on a string (or String instance) — no import required.
Library declarations
JavaScript
str.replace(pattern, replacement)
Parameters
Parameter
Description
pattern
Required. Substring or RegExp to find
replacement
Required. New text, or a function returning text
Return Value
Return Value
Description
*
The value returned by string.replace().
JavaScript Examples
Example 1 — Replace the First Match
A plain string pattern swaps only the first occurrence.
Search for a literal."world" is treated as plain text, not a regex.
Swap the first hit. Only the first "world" becomes "JS".
Leave the rest. The second "world" stays put.
Keep the original.text is unchanged — use the return value.
Example 2 — Replace Every Match with /g
Add the global flag (and i for case-insensitive).
JavaScript
const str = "Apples are round, and apples are juicy.";
console.log(str.replace(/apples/gi, "oranges"));
// "oranges are round, and oranges are juicy."
console.log("a a a".replace(/a/g, "b"));
// "b b b"
Capture with parentheses.(\w+) is group 1; the next (\w+) is group 2.
Reuse in the replacement.$1 and $2 insert those groups.
Reorder freely."$2, $1" puts last name first.
Same idea for dates. Year/month/day groups rearrange into MM/DD/YYYY.
Compatibility
Browser & Runtime Support
String.prototype.replace() has been in JavaScript since the early language and is universal across browsers and runtimes.
✓ Baseline · Universal
String.prototype.replace()
Supported in all modern browsers, Internet Explorer, and every current Node.js release. Safe everywhere.
100%Browser support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
String.replace()Universal
Bottom line: Use confidently in any environment. For replacing every string match with a clearer API, prefer replaceAll() where available (ES2021+).
Frequently Asked Questions
replace(pattern, replacement) returns a new string where matches of pattern are swapped for replacement. A plain string pattern replaces only the first match. A RegExp with the g flag can replace every match.
No. Strings are immutable. replace() returns a new string; the original stays the same.
Use a regular expression with the g (global) flag, or use String.prototype.replaceAll() for a clearer all-matches API.
Yes. Pass a function as the second argument. It runs for each match and its return value becomes the replacement text.
When pattern is a RegExp with capturing groups, $1 inserts the first group, $2 the second, and so on. $$ inserts a literal dollar sign. $& inserts the whole match.
replace() returns a new string with the same contents as the original — nothing is changed.