By the end of this tutorial, you’ll use Lodash’s _.replace() confidently in real string workflows.
01
Core Syntax
Call _.replace(string, pattern, replacement).
02
RegExp
Use /pattern/g for all matches.
03
Callbacks
Dynamic replacement functions.
04
Templates
Swap {placeholder} values.
05
Sanitize
Mask sensitive data patterns.
06
Production Tips
Escape regex when matching literals.
Fundamentals
What Is _.replace()?
_.replace() searches a string for a pattern (string or RegExp) and substitutes a replacement string or function. It mirrors String.prototype.replace() with lodash’s consistent API.
💡
Beginner tip
Use a RegExp with the g flag to replace all matches—_.replace(text, /foo/g, 'bar'). A string pattern replaces only the first occurrence.
Foundation
📝 Syntax
javascript
_.replace(string, pattern, replacement)
Syntax Rules
string — The source string to search.
pattern — String or RegExp to match.
replacement — Replacement string or callback function.
Return value — New string with substitutions applied.
Global flag — Use /pattern/g for all matches with RegExp.
javascript
import replace from "lodash/replace";
const result = replace("Hello world", "world", "lodash");
// -> "Hello lodash"
Cheat Sheet
⚡ Quick Reference
Task
Code pattern
Result
First match
_.replace(s, 'a', 'b')
Single substitution
All matches
_.replace(s, /a/g, 'b')
Global regex
Case insensitive
_.replace(s, /hi/i, 'Hello')
i flag
Callback
_.replace(s, /\d+/g, n => ...)
Dynamic replace
Template
_.replace(tpl, /{(\w+)}/g, ...)
Placeholders
Native alt
str.replace(...)
Built-in equivalent
Mutates?
No
Returns new string
Pattern
String|RegExp
Flexible matching
Global
/g flag
All occurrences
Native
replace()
Same behavior
Reference
🧰 Parameters
stringRequired
Text to search within.
_.replace(msg, 'old', 'new')
patternRequired
Substring or RegExp to find.
_.replace(s, /\s+/g, '-')
replacementRequired
New text or replacer function.
_.replace(s, /x/g, 'y')
return valueNew string
With replacements applied.
const clean = _.replace(raw, ...)
Hands-On
Examples Gallery
Practical _.replace() patterns with copy-ready code and interactive Try It Yourself labs.
📚 Getting Started
Replace a word in a greeting.
Example 1 — Basic substring replacement
Replace the first hello with hi.
javascript
const text = "Hello world, hello universe!";
const result = _.replace(text, "hello", "hi");
console.log(result);
// -> "Hello world, hi universe!"
Built-in replace behaves the same for basic cases.
javascript
const text = "foo bar foo";
const native = text.replace(/foo/g, "baz");
const lodash = _.replace(text, /foo/g, "baz");
// both -> "baz bar baz"
📤 Console output:
Both approaches yield: baz bar baz
How It Works
Use lodash when chaining in functional pipelines.
Compare
📋 Related string operations
Topic
_.replace
replace()
replaceAll()
split+join
Pattern
String|RegExp
String|RegExp
String|RegExp
String only
All matches
Regex /g
Regex /g
Yes (string)
Yes
Callback
Yes
Yes
No
No
Mutates
No
No
No
No
Best for
Lodash pipelines
Native code
Simple global
Literal strings
🧠 How _.replace() Works
1
Receive string
Source text and pattern.
Input
2
Find matches
String finds first; RegExp uses flags.
Match
3
Apply replacement
Substitute string or invoke callback.
Replace
=
Return result
New string; original unchanged.
Done
Important
📝 Notes
String patterns replace only the first occurrence.
Use RegExp with g flag for global replacement.
Replacement can be a function receiving match details.
Does not mutate the original string.
For simple literal global replace, replaceAll() (ES2021) works too.
Escape regex metacharacters when matching literals—see _.escapeRegExp().
Wrap Up
Conclusion
_.replace() handles substring and regex substitutions in lodash pipelines. Choose the right pattern type, use the global flag when needed, and consider callback replacements for dynamic transforms.