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() method console.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() method console.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() method console.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