to-regex-range
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
is-number | 7.0.0 | 3.64 kB | MIT | prod |
The npm package "to-regex-range" provides a JavaScript utility for creating a regex-compatible source string for matching numerical ranges. It requires two numbers as arguments and is capable of handling a large variety of number ranges, making it a flexible and robust solution for match number ranges in applications. This package has been validated against more than 2.78 million test assertions, providing users with a high level of assurance about the quality and accuracy of its output.
To utilise the "to-regex-range" npm package, first, it needs to be installed into your JavaScript project. This can be performed by running the command npm install --save to-regex-range
in your terminal. Once installed, you can import this library into your JavaScript file using const toRegexRange = require('to-regex-range');
.
This function requires two numbers (or strings that can be converted into numbers) as parameters representing the min and max of the range. The output is a string that can be used to create a new RegExp instance.
const toRegexRange = require('to-regex-range');
const source = toRegexRange('15', '95');
//=> 1[5-9]|[2-8][0-9]|9[0-5]
const regex = new RegExp(`^${source}$`);
console.log(regex.test('14')); //=> false
console.log(regex.test('50')); //=> true
console.log(regex.test('94')); //=> true
console.log(regex.test('96')); //=> false
In this given example, the toRegexRange
function creates a regex-compatible string to match any number within the range 15 to 95. This is then used to create a new RegExp, which can be tested against different values.
The official documentation for the "to-regex-range" npm package is included within the README on the project's GitHub repository. It provides detail on installation, usage, options, examples and also includes a section on how the order of arguments can be flipped to create a valid range. Furthermore, this README is updated with each new version of the package, ensuring users have accurate information that matches the current state of the package.