Replace matches for a pattern in a string with a replacement (string or function). Part of Lodash String (stable — not deprecated). Patterns may be plain strings or RegExp.
Pass the string and options shown below. Check the return type before you chain another helper.
Syntax
Call _.replace. Start with this line.
Lodash
_.replace([string=''], pattern, replacement)
Call
What it does
_.replace([string=''], pattern, replacement)
Primary signature.
_.replace(string)
Transform or inspect a string.
Prefer import replace from "lodash/replace" when you want a smaller bundle.
String pattern. Would be case-sensitive and first-only.
Compatibility
Browser & Runtime Support
_.replace is a Lodash String helper available in 4.x, documented as based on String#replace. It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native string.replace is universal; Lodash helps in pipelines and consistent coercion of the source to a string.
✓ Stable · Lodash 4.x
_.replace()
Available in [email protected] full builds and the lodash/replace modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.
100%With Lodash 4.x
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
_.replaceLodash 4.x
Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import replace from "lodash/replace" for smaller builds. Use /g when you need every match.
Frequently Asked Questions
_.replace([string=''], pattern, replacement) replaces matches for pattern in string with replacement. Official: _.replace('Hi Fred', 'Fred', 'Barney') → 'Hi Barney'.
Yes. Lodash documents that this method is based on String#replace — same string/RegExp patterns and string or function replacements.
No. Like native replace, a string pattern replaces only the first match. Use a RegExp with the g flag to replace all.
Yes. The function receives the same arguments as native String#replace and should return the replacement string.
No. Strings are immutable. It returns a new string.
Use import replace from "lodash/replace" or require("lodash/replace") for tree-shaking. Full build: import _ from "lodash" then _.replace.