Lodash _.isRegExp() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.isRegExp() method is used to find whether the given value is a regular expression or not. It returns True if the given value is a regular expression. Otherwise, it returns false.
Syntax:
_.isRegExp(value)
Parameters: This method accepts a single parameter as mentioned above and described below:
value: This parameter holds the value to check.
Return Value: This method returns true if the value is a regular expression, else false.
Note: Here, const _ = require(‘lodash’) is used to import the lodash library into the file.
Example 1: Passing a regular expression to the _.isRegExp() function
Here, the object starts and ends with ‘/’, therefore it is a regular expression. Hence, the result is true.
javascript
// Requiring the lodash library const _ = require("lodash"); // Use of _.isRegExp() methodconsole.log(_.isRegExp(/gfg/)); |
Output:
true
Example 2: Passing a string to the _.isRegExp() function
Since a string is not a regular expression therefore, the output will be false.
javascript
// Requiring the lodash library const _ = require("lodash"); // Use of _.isRegExp() methodconsole.log(_.isRegExp('gfg')); |
Output:
false
Example 3: Passing a string with ‘/’ to _.isRegExp() function
Hence the overall object is a string., the output will be false.
javascript
// Requiring the lodash library const _ = require("lodash"); // Use of _.isRegExp() methodconsole.log(_.isRegExp('/gfg/')); |
Output:
false
Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.
Reference: https://lodash.com/docs/4.17.15#isRegExp
Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!


