Lodash _.escapeRegExp() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
String

Definition

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=''])
CallWhat 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

ParameterDescription
stringOptional. Text to escape

Return Value

Return ValueDescription
*New string safe for RegExp source

Lodash Examples

Example 1 — Official markdown-style link

Escape brackets, parens, and dots in a URL-like string.

Lodash
_.escapeRegExp('[lodash](https://lodash.com/)');
// → '\[lodash\]\(https://lodash\.com/\)'
Try It Yourself

How It Works

  1. Input. Contains [ ] ( ) . — all RegExp specials.
  2. Escape. Each gets a leading \.
  3. Use. Safe as new RegExp(...) source.
  4. Official. Same example as the Lodash docs.

Example 2 — User input search

Keep parentheses literal in a query.

Lodash
var userInput = '(cat|dog)';
var escaped = _.escapeRegExp(userInput);
var regex = new RegExp(escaped);

escaped;
// → '\(cat\|dog\)'

regex.test('(cat|dog)');
// → true
Try It Yourself

How It Works

  1. Raw. | would mean “or”; () would group.
  2. escapeRegExp. Makes them literals.
  3. test. Matches the exact user string.
  4. Why. Unescaped user input can throw or match wrongly.

Example 3 — Literal global replace

Replace $ and ^ as characters, not anchors.

Lodash
var text = 'cost $5 ^ tip';

text
  .replace(new RegExp(_.escapeRegExp('$'), 'g'), '_')
  .replace(new RegExp(_.escapeRegExp('^'), 'g'), '_');
// → 'cost _5 _ tip'
Try It Yourself

How It Works

  1. $ / ^. Normally mean end / start of string.
  2. escape. Turns them into literal matchers.
  3. g flag. Replaces every occurrence.
  4. Result. Characters swapped without anchor behavior.

Example 4 — Why escape dots

Unescaped . matches any character.

Lodash
var file = 'report.pdf';

new RegExp('report.pdf').test('reportXpdf');
// → true   // '.' matches 'X' — too loose

new RegExp(_.escapeRegExp('report.pdf')).test('reportXpdf');
// → false

new RegExp(_.escapeRegExp('report.pdf')).test('report.pdf');
// → true
Try It Yourself

How It Works

  1. Raw. . is “any character” in RegExp.
  2. Bug. reportXpdf can wrongly match.
  3. escapeRegExp. Makes . mean a real dot.
  4. Lesson. Always escape filenames and user text.

Example 5 — vs _.escape

HTML vs RegExp — different jobs.

Lodash
var input = 'a+b (test)';

_.escape(input);
// → 'a+b (test)'          // no HTML specials

_.escapeRegExp(input);
// → 'a\\+b \\(test\\)'
Try It Yourself

How It Works

  1. escape. Only HTML & < > " '.
  2. escapeRegExp. Escapes + and parentheses.
  3. Pick. HTML output → escape; RegExp → escapeRegExp.
  4. See. _.escape.

Browser & Runtime Support

_.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 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
_.escapeRegExp Lodash 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 ''.

Next: _.kebabCase()

Turn titles and identifiers into kebab-case for URLs and CSS.

_.kebabCase() →

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.

9 people found this page helpful