is-regex
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
call-bind | 1.0.7 | 21.6 kB | MIT | prod | |
has-tostringtag | 1.0.2 | 6.31 kB | MIT | prod |
The is-regex
NPM package provides a utility function for validating if given value is a JavaScript regular expression. It supports cross-realm/iframe operations and can function regardless of ES6 @@toStringTag. This tool can be incredibly valuable for developers, in validating user inputs, or distinguishing regular expressions within the data in JavaScript development scenarios.
To use the is-regex
NPM package, first install it using npm install is-regex
. Then, require the package in your JavaScript file with the require
function. Here's an example of how you can use is-regex
:
var isRegex = require('is-regex');
var assert = require('assert');
// Test non-regex values
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({}));
// Test actual regex values
assert.ok(isRegex(/a/g));
assert.ok(isRegex(new RegExp('a', 'g')));
In this example, isRegex
is first applied to a variety of non-regex data types; in each case the assert.notOk()
function confirms that the result is false. However, when isRegex
is applied to arguments that do constitute regex patterns, assert.ok()
affirms that a truthy value is returned.
The is-regex
package documentation is primarily available on the GitHub page: https://github.com/inspect-js/is-regex. This page provides an overview of the package, example usage, as well as a link to the NPM listing. This repository includes a description of the module, a simple usage example, and instructions for running the packaged tests. For more detailed usage scenarios, consider forking the repository and running the tests yourself.