VOOZH about

URL: https://www.geeksforgeeks.org/javascript/how-to-determine-if-a-value-is-regular-expression-using-lodash/

⇱ How to Determine if a value is Regular Expression using Lodash? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Determine if a value is Regular Expression using Lodash?

Last Updated : 23 Jul, 2025

When working with dynamic data in JavaScript, it's often necessary to validate the types of values, including checking whether a value is a regular expression (RegExp). Lodash provides an elegant and easy-to-use utility function for this: _.isRegExp. This function allows us to verify if a given value is a regular expression or not.

Prerequisites

Install Lodash:

Open your terminal and navigate to your project directory. Run the following command to install Lodash:

npm install lodash

Approach

The _.isRegExp function provided by Lodash is specifically designed to check if a value is a regular expression. This method is part of Lodash’s suite of type-checking utilities and provides a straightforward way to perform this check.

Here are several examples demonstrating how to use _.isRegExp to check if a value is a regular expression.

Example 1:This illustrates regex which is a regular expression object, so _.isRegExp(regex) returns true.

Output:

true

Example 2: This illustrates str as a string, not as a regular expression. _.isRegExp(str) returns false.

Output:

false

Example 3: Checking an Object, In this case, obj is a plain JavaScript object and _.isRegExp returns false.

Ouptut:

false
Comment