Node.js util.types.isRegExp() Method

The util.types.isRegExp() method of util module which is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed value is a regular expression.

Syntax:

util.types.isRegExp( value )

Parameters: This method accepts a single parameter value which holds any value i.e instance of any module.

Return value: THis method returns a Boolean value i.e true if the passed value is a regular expression otherwise returns false.

Below examples illustrate the use of util.types.isRegExp() method in Node.js:

Example 1:




// Node.js program to demonstrate the   
// util.types.isRegExp() method
  
// It includes util module
const util = require('util');
  
// Returns false as the passed instance
// is not regular expression
console.log(util.types.isRegExp(new Map()));
  
// Returns true as the passed instance
// is regular expression
console.log(util.types.isRegExp(new RegExp('xyz')));


Output:

false
true

Example 2:




// Node.js program to demonstrate the   
// util.types.isRegExp() method
  
// It includes util module
const util = require('util');
  
// Returns false as the passed instance
// is not regular expression
console.log(util.types.isRegExp(new Map()));
  
// Returns true as the passed instance
// is regular expression
console.log(util.types.isRegExp(new RegExp('xyz')));
  
// Returns true as the passed instance
// is regular expression
console.log(util.types.isRegExp(/abc/));
  
// Returns false as the passed instance
// is not regular expression
console.log(util.types.isRegExp(new Int32Array()));
  
// Returns true as the passed instance
// is regular expression 
console.log(util.types.isRegExp(/ab+c/));


Output:

false
true
true
false
true

Reference: https://nodejs.org/api/util.html#util_util_types_isregexp_value