Escapes RegExp special characters in a string — ^ $ \ . * + ? ( ) [ ] { } | — so you can build a new RegExp(...) that matches the text literally. Part of Lodash String (stable — not deprecated).
Pass the string and options shown below. Check the return type before you chain another helper.
Syntax
Call _.escapeRegExp. Start with this line.
Lodash
_.escapeRegExp([string=''])
Call
What it does
_.escapeRegExp([string=''])
Primary signature.
_.escapeRegExp(string)
Transform or inspect a string.
Prefer import escapeRegexp from "lodash/escapeRegexp" when you want a smaller bundle.
Library declarations
Lodash
_.escapeRegExp([string=''])
Parameters
Parameter
Description
string
Optional. Text to escape
Return Value
Return Value
Description
*
New string safe for RegExp source
Lodash Examples
Example 1 — Official markdown-style link
Escape brackets, parens, and dots in a URL-like string.
_.escapeRegExp is a Lodash 4.x String helper. It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash or lodash/escapeRegExp. Native idea: escape metacharacters yourself before new RegExp.
✓ Stable · Lodash 4.x
_.escapeRegExp()
Available in [email protected] and lodash/escapeRegExp 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
_.escapeRegExpLodash 4.x
Bottom line: Safe for production. Escape any untrusted text before new RegExp().
Frequently Asked Questions
_.escapeRegExp([string='']) escapes RegExp specials (^ $ \ . * + ? ( ) [ ] { } |) so the string is treated literally inside new RegExp(). Official: _.escapeRegExp('[lodash](https://lodash.com/)') → '\[lodash\]\(https://lodash\.com/\)'.
Whenever you build a RegExp from user input or any text that may contain regex syntax.
No. Strings are immutable. It returns a new escaped string.
_.escape encodes HTML entities. _.escapeRegExp escapes RegExp metacharacters.
Yes for simple substring checks. Use escapeRegExp when you need RegExp flags, replace-all, or anchors.
Lodash coerces missing values to '', so _.escapeRegExp(null) returns ''.