What is is-regex?
The is-regex npm package is a utility for checking if a value is a regular expression. It is primarily used to determine whether a given value is an instance of a RegExp object.
What are is-regex's main functionalities?
Check if a value is a RegExp
This feature allows you to verify if a value is a regular expression by returning a boolean result. It's useful when you need to ensure that a variable contains a valid RegExp object before performing operations that are specific to regular expressions.
const isRegex = require('is-regex');
console.log(isRegex(/abc/)); // true
console.log(isRegex('abc')); // false
Other packages similar to is-regex
is-regexp
Similar to is-regex, is-regexp is a package that checks if a value is a regular expression. It provides a similar functionality but may have different implementation details or dependencies.
lodash.isregexp
This is a Lodash module to check if a value is classified as a RegExp object. While it offers similar functionality, it comes from the Lodash library which is a broader utility toolkit and might be preferred in projects already using Lodash.
is-regex
Is this value a JS regex?
This module works cross-realm/iframe, and despite ES6 @@toStringTag.
Example
var isRegex = require('is-regex');
var assert = require('assert');
assert.notOk(isRegex(undefined));
assert.notOk(isRegex(null));
assert.notOk(isRegex(false));
assert.notOk(isRegex(true));
assert.notOk(isRegex(42));
assert.notOk(isRegex('foo'));
assert.notOk(isRegex(function () {}));
assert.notOk(isRegex([]));
assert.notOk(isRegex({}));
assert.ok(isRegex(/a/g));
assert.ok(isRegex(new RegExp('a', 'g')));
Tests
Simply clone the repo, npm install
, and run npm test