Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

lodash
communitycreator

How to check if a value is a regular expression using Lodash

Shubham Singh Kshatriya

Overview

In Lodash, we can check if a value is a regular expression using the _.isRegExp() method. This method checks if the specified value is a RegExp object.

Syntax

_.isRegExp(value);
Syntax of _.isRegExp()

Parameters

This method accepts the following parameter:

  • value: This is the value to be checked.

Return value

This method returns true if the value is a regular expression, and false if the value is not a regular expression.

Example

Let’s look at an example of checking if a value is a regular expression using the _.isRegExp() method in the code snippet below.

Console

Explanation

In the HTML tab, we see the following:

  • Line 5: We import the lodash script.

In the JavaScript tab, we see the following:

  • Line 2–5: We create two variables, value1 and value2, and initialize them.
  • Line 9: We check if value1 is a regular expression and print the result on the console.
  • Line 10: We check if value2 is a regular expression and print the result on the console.

RELATED TAGS

lodash
communitycreator

CONTRIBUTOR

Shubham Singh Kshatriya
RELATED COURSES

View all Courses

Keep Exploring