to-regex
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
define-property | 2.0.2 | 3.95 kB | MIT | prod | |
extend-shallow | 3.0.2 | 3.13 kB | MIT | prod | |
regex-not | 1.0.2 | 3.28 kB | MIT | prod | |
safe-regex | 1.1.0 | 2.81 kB | MIT | prod |
The "to-regex" npm module is a handy tool with a principal task of converting a given string or an array of strings into a regular expression. This can be particularly useful in situations where you need to create dynamic regular expressions based on inputs.
Using "to-regex" in your Javascript project is quite straightforward. Firstly, you need to include the package in your project. You can do it by using npm, Node.js package manager, as shown below:
$ npm install --save to-regex
After successfully installing the module, you can utilize it within your project as follows:
var toRegex = require('to-regex');
console.log(toRegex('foo')); //=> /^(?:foo)$/
console.log(toRegex('foo', {negate: true})); //=> /^(?:(?:(?!^(?:foo)$).)*)$/
console.log(toRegex('foo', {contains: true})); //=> /(?:foo)/
console.log(toRegex(['foo', 'bar'], {negate: true})); //=> /^(?:(?:(?!^(?:(?:foo)|(?:bar))$).)*)$/
console.log(toRegex(['foo', 'bar'], {negate: true, contains: true})); //=> /^(?:(?:(?!(?:(?:foo)|(?:bar))).)*)$/
The "to-regex" function accepts two parameters. The first parameter is the input string or an array of strings that you wish to convert into a regex. The second parameter is an optional object where you can supply various configuration options to customize the generated regex, such as "contains", "negate", "nocase", "flags", "cache", and "safe".
The documentation for the "to-regex" module is available in the readme of the GitHub repository where the project is maintained. You can access the module's documentation by visiting the to-regex GitHub repository. These documents include detailed explanations for installation, usage, and available configuration options.