Underscore.js _.isRegExp() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Object

Definition

Returns true if object is a RegExp (official Underscore docs). Literals like /moe/ and new RegExp("moe") pass; strings and other types do not. Stable helper — not deprecated.

Pass the required arguments shown below. Underscore object helpers usually return a new value — check whether the source object is mutated.

Syntax

Call _.isRegExp. Start with this line.

Underscore
_.isRegExp(object)
CallWhat it does
_.isRegExp(object)Primary signature.
_.isRegExp(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.isRegExp(object)

Parameters

ParameterDescription
objectRequired. Value to test

Return Value

Return ValueDescription
*true or false

Underscore.js Examples

Example 1 — Official: /moe/

The docs check a real RegExp literal.

Underscore
_.isRegExp(/moe/);
// → true
Try It Yourself

How It Works

  1. Literal. /moe/ creates a RegExp value at parse time.
  2. Tag check. Underscore sees [object RegExp] → true.
  3. Official pair. This is the example from the Underscore documentation.
  4. Read-only. Nothing is mutated; you only get a boolean.

Example 2 — Literal vs new RegExp()

Both ways of building a RegExp pass the same check.

Underscore
_.isRegExp(/abc/i);
// → true

_.isRegExp(new RegExp("abc", "i"));
// → true
Try It Yourself

How It Works

  1. Literal form. /abc/i is a RegExp with the ignore-case flag.
  2. Constructor form. new RegExp("abc", "i") builds the same kind of object.
  3. Same answer. Both have the RegExp tag → both return true.
  4. When to use which. Literals for fixed patterns; new RegExp when the pattern is dynamic.

Example 3 — Strings and Other Types Fail

A pattern written as text is still a string — not a RegExp.

Underscore
_.isRegExp("/moe/");     // → false
_.isRegExp("moe");       // → false
_.isRegExp(function () {}); // → false
_.isRegExp({});          // → false

_.isString("/moe/");     // → true  (narrower check for text)
Try It Yourself

How It Works

  1. Look-alike string. "/moe/" has slashes as characters — still typeof "string".
  2. Plain pattern. "moe" is text you could pass to new RegExp, not a RegExp yet.
  3. Other types. Functions and plain objects are not RegExps.
  4. Need a string check? Use _.isString.

Example 4 — Guard Before .test()

Accept a RegExp or a pattern string, then test safely.

Underscore
function matches(value, pattern) {
  var re = _.isRegExp(pattern) ? pattern : new RegExp(pattern);
  return re.test(value);
}

matches("hello moe", /moe/);   // → true
matches("hello moe", "moe");   // → true
matches("hello", /moe/);       // → false
Try It Yourself

How It Works

  1. Branch. If pattern is already a RegExp, use it as-is.
  2. Coerce. Otherwise wrap the string with new RegExp(pattern).
  3. Test. .test(value) returns whether the text matches.
  4. Pattern. Type-check first — then call regex methods.

Browser & Runtime Support

_.isRegExp ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.isRegExp()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.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 Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.isRegExp(object) returns true only when the value is a RegExp — a literal such as /moe/ or new RegExp("moe"). Strings, functions, numbers, and plain objects return false.
_.isRegExp(/moe/) returns true. The docs use a real RegExp literal on purpose — that is the type the helper looks for.
No. "/moe/" or "moe" is still a string. Build a RegExp with a literal or new RegExp(...) first, then call isRegExp on the result.
typeof /a/ is "object", so typeof is too broad. Underscore checks the internal [object RegExp] tag and returns true only for real RegExp values.
Use it as a guard before calling .test() or .exec(), or when an API may accept either a string or a RegExp and you need to branch safely.
No. It is a stable Underscore type helper. Load Underscore 1.x (CDN or npm) and call _.isRegExp(value).

Next: _.isError()

Learn how to detect Error objects — including TypeError and custom errors.

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