Lodash _.replace() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
String

Definition

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)
CallWhat 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.

Library declarations
Lodash
_.replace([string=''], pattern, replacement)

Parameters

ParameterDescription
stringOptional. Source text (default '')
patternRequired. String or RegExp to match
replacementRequired. String or function return value

Return Value

Return ValueDescription
*New string with replacements

Lodash Examples

Example 1 — Official: Replace a Substring

Swap one plain string for another.

Lodash
_.replace("Hi Fred", "Fred", "Barney");
// → "Hi Barney"
Try It Yourself

How It Works

  1. Find. Looks for the substring "Fred".
  2. Replace. Writes "Barney" in its place.
  3. Rest. "Hi " stays unchanged.
  4. Docs. Matches the official Lodash example.

Example 2 — String Pattern Replaces First Match Only

Same rule as native String#replace.

Lodash
_.replace("a-a-a", "a", "b");
// → "b-a-a"
Try It Yourself

How It Works

  1. First. Only the leading "a" is replaced.
  2. Rest. Later "a" characters remain.
  3. Why. String patterns are not global.
  4. All. Use a regex with g (next example).

Example 3 — Replace All with /g

A global RegExp replaces every match.

Lodash
_.replace("a-a-a", /a/g, "b");
// → "b-b-b"
Try It Yourself

How It Works

  1. Pattern. /a/g matches every a.
  2. Each. Every match becomes "b".
  3. Result. "b-b-b".
  4. Tip. Escape special regex characters when building patterns from user input.

Example 4 — Replacement Function

Compute the new text from each match.

Lodash
_.replace("id-12-id-34", /id-(\d+)/g, function (match, num) {
  return "ID(" + num + ")";
});
// → "ID(12)-ID(34)"
Try It Yourself

How It Works

  1. Match. Captures digits after id-.
  2. Callback. Receives full match and capture groups.
  3. Return. Builds "ID(…)" for each hit.
  4. Same API. Arguments match native String#replace.

Example 5 — Case-Insensitive Replace

Use the i flag (and g for all).

Lodash
_.replace("Hello HELLO hello", /hello/gi, "hi");
// → "hi hi hi"
Try It Yourself

How It Works

  1. i. Ignores letter case.
  2. g. Replaces every match.
  3. Result. All three variants become "hi".
  4. String pattern. Would be case-sensitive and first-only.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.replace Lodash 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.

Next: _.snakeCase()

Convert a string to snake_case.

_.snakeCase() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful