Lodash _.isRegExp() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Lang

Definition

Checks if value is classified as a RegExp object. Part of Lodash Lang (stable — not deprecated). Note: a string like "/abc/" is not a RegExp — official docs show _.isRegExp('/abc/') → false.

Pass the value you want to inspect or convert. Read the return type carefully before you use the result.

Syntax

Call _.isRegExp. Start with this line.

Lodash
_.isRegExp(value)
CallWhat it does
_.isRegExp(value)Primary signature.

Prefer import isRegexp from "lodash/isRegexp" when you want a smaller bundle.

Library declarations
Lodash
_.isRegExp(value)

Parameters

ParameterDescription
valueRequired. Value to check

Return Value

Return ValueDescription
*true if RegExp, else false

Lodash Examples

Example 1 — Official: Literal vs String

Only a real RegExp instance returns true.

Lodash
console.log(_.isRegExp(/abc/));   // true
console.log(_.isRegExp('/abc/')); // false
Try It Yourself

How It Works

  1. Literal. /abc/ creates a RegExp object → true.
  2. String. '/abc/' is text that only looks like a pattern → false.
  3. Docs. Matches the official Lodash examples exactly.
  4. Tip. Compile strings before treating them as regexes.

Example 2 — new RegExp(...) Also Passes

Constructor-built regexes are real RegExp objects too.

Lodash
var re = new RegExp("abc", "i");

console.log(_.isRegExp(re)); // true
console.log(re.test("ABC")); // true
Try It Yourself

How It Works

  1. Construct. new RegExp("abc", "i") builds a RegExp.
  2. Type check. isRegExp → true.
  3. Use. Flags work as usual — test("ABC") succeeds.
  4. When. Prefer the constructor when the pattern comes from a variable.

Example 3 — Distinguish String Patterns from RegExp

Pair isRegExp with isString when input may be either.

Lodash
function asRegExp(value) {
  if (_.isRegExp(value)) {
    return value;
  }
  if (_.isString(value)) {
    return new RegExp(value);
  }
  return null;
}

console.log(_.isRegExp(asRegExp(/hi/)));     // true
console.log(_.isRegExp(asRegExp("hi")));     // true
console.log(asRegExp(42));                  // null
Try It Yourself

How It Works

  1. Already RegExp. Return it unchanged.
  2. String pattern. Compile with new RegExp.
  3. Other types. Reject with null.
  4. Use. Handy for APIs that accept either form.

Example 4 — Look-Alike Objects Fail

Shape is not enough — it must be a real RegExp.

Lodash
var fake = { source: "abc", flags: "i", test: function () { return true; } };

console.log(_.isObjectLike(fake)); // true
console.log(_.isRegExp(fake));     // false
console.log(_.isRegExp(/abc/i));   // true
Try It Yourself

How It Works

  1. Fake object. Has source / test but is a plain object.
  2. Object-like. Passes the broad object check.
  3. isRegExp. Requires RegExp classification → false.
  4. Real literal. /abc/i passes.

Example 5 — Guard Before Calling .test()

Avoid runtime errors when the matcher is unknown.

Lodash
function matches(matcher, text) {
  if (!_.isRegExp(matcher)) {
    return false;
  }
  return matcher.test(text);
}

console.log(matches(/ad/, "Ada"));   // true
console.log(matches("/ad/", "Ada")); // false
console.log(matches(null, "Ada"));   // false
Try It Yourself

How It Works

  1. Guard. Skip .test() unless the matcher is a RegExp.
  2. Literal. Real regex matches "Ada".
  3. String / null. Fail safely without throwing.
  4. Why. Calling .test on a string would throw a TypeError.

Browser & Runtime Support

_.isRegExp is a Lodash Lang helper available in 4.x (and earlier builds). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native engines offer instanceof RegExp; Lodash uses tag-based classification for more consistent cross-realm checks.

✓ Stable · Lodash 4.x

_.isRegExp()

Available in [email protected] full builds and the lodash/isRegExp 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
_.isRegExp Lodash 4.x

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import isRegExp from "lodash/isRegExp" for smaller builds. Remember: '/abc/' is a string, not a RegExp.

Frequently Asked Questions

_.isRegExp(value) returns true when value is classified as a RegExp object — for example /abc/ or new RegExp('abc'). Look-alike strings return false.
That value is a string. Official Lodash docs show it as false. Compile it with new RegExp(...) or use a literal /abc/ if you need a real RegExp.
Usually aligned in one realm. Lodash uses internal tagging so classification stays consistent across environments where instanceof can fail (for example across iframes).
No. Only real RegExp instances matter — ordinary objects that mimic a regex shape still fail.
JSON does not serialize regex. API payloads usually send a string pattern you must compile yourself with new RegExp.
Use import isRegExp from "lodash/isRegExp" or require("lodash/isRegExp") so bundlers can tree-shake unused Lodash code. Full build: import _ from "lodash" then _.isRegExp.

Next: _.isSafeInteger()

Check whether a value is a safe integer number.

_.isSafeInteger() 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