Lodash _.isRegExp() method

Beginner
⏱️ 5 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.isRegExp(value) confirms a real RegExp instance.
  • Why strings and ordinary objects fail even when they look regex-ish.
  • How literals and new RegExp() both qualify.
  • Where guards belong before calling .test, .exec, or string methods that expect a pattern.

Prerequisites

You know a regex literal (/pattern/) differs from a string that holds characters.

  • You have used RegExp or regex literals in matching code.
  • Try-it labs load lodash from the CDN.

Overview

Use _.isRegExp when APIs accept “pattern or string” unions and you must branch to safe regex operations only for genuine RegExp objects.

Native instances

Literals and new RegExp() both register as RegExp.

Strings fail

A pattern-shaped string is still a string per lodash docs.

Guard rails

Validate before .test / match pipelines assume a regex.

Syntax

javascript
_.isRegExp(value)
  • value: any value to test.
  • Returns: true if value is a RegExp object; otherwise false.
1

Literals and new RegExp()

Both creation styles produce instances lodash recognizes.

javascript
import isRegExp from "lodash/isRegExp";

console.log(
  "literal: " + isRegExp(/pattern/) + "\n" +              // true
  "ctor: " + isRegExp(new RegExp("dynamic", "i"))       // true
);
Try it Yourself
2

String patterns and plain objects

The lodash docs emphasize _.isRegExp("/abc/") === false—a string is never classified as a regexp.

javascript
import isRegExp from "lodash/isRegExp";

console.log(
  "slashStr: " + isRegExp("/abc/") + "\n" +        // false (lodash docs)
  "plainObj: " + isRegExp({ source: "x" })        // false
);
Try it Yourself
3

Primitives and nullish values

Numbers and booleans are out; null and undefined never qualify.

javascript
import isRegExp from "lodash/isRegExp";

console.log(
  "num: " + isRegExp(42) + "\n" +               // false
  "nullVal: " + isRegExp(null) + "\n" +        // false
  "undef: " + isRegExp(undefined)             // false
);
Try it Yourself

📋 _.isRegExp vs related checks

API / patternBehavior
_.isRegExp(x)true only for actual RegExp instances.
x instanceof RegExpSimilar in one realm; breaks across frames/realms sometimes.
typeof x === "object"Too broad—arrays and dates also satisfy typeof object.
Object.prototype.toString.call(x)Manual [object RegExp] tag—lodash wraps this idea.

Pitfalls to avoid

Strings

Do not coerce blindly

User input that “looks like” /.../ is still a string until you compile it explicitly.

JSON

No regex in JSON

Serialization drops regex; deserialize patterns as strings and rebuild with new RegExp when needed.

APIs

Union types

When a parameter accepts string | RegExp, branch on _.isRegExp before assuming regex methods exist.

❓ FAQ

That value is a string. Lodash checks for an actual RegExp object—use new RegExp(...) or a literal /abc/ if you need instance semantics.
Usually aligned in one realm; lodash uses internal tagging so behavior stays consistent across environments where instanceof can break.
Only real RegExp instances matter—ordinary objects that mimic regex shape still fail.
JSON does not serialize regex—if you receive one from an API it will usually be a string you must compile yourself.

Summary

  • Purpose: guarantee a value is a native-style RegExp before regex-specific work.
  • Remember: strings and POJO stand-ins always return false.
  • Next: explore more on Lodash _.isSafeInteger().
Did you know?

The lodash docs show _.isRegExp("/abc/") as false—a string that looks like a pattern is still just a string until you pass it to new RegExp().

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.

6 people found this page helpful