What is to-regex-range?
The to-regex-range npm package is designed to generate regular expressions for matching numeric ranges. It is useful for creating regex patterns that can match against specific ranges of numbers, allowing for precise control over numeric input validation, among other applications.
What are to-regex-range's main functionalities?
Generating regex for simple numeric ranges
This feature allows you to generate a regex pattern for a simple numeric range, such as from 5 to 10. The resulting regex will match any number within this range.
const toRegexRange = require('to-regex-range');
const regex = toRegexRange('5', '10');
console.log(regex); // => '5|6|7|8|9|10'
Creating regex with zero-padding
This feature enables the generation of regex patterns that account for zero-padded numbers, useful for matching numbers within a range where the number of digits is consistent.
const toRegexRange = require('to-regex-range');
const regex = toRegexRange('001', '100', { capture: true, pad: true });
console.log(regex); // Example output: '(0[0-9]{2}|1[0-9]{2})'
Generating regex for complex ranges with options
This feature allows for the creation of regex patterns for more complex numeric ranges with additional options, such as disabling the relaxation of leading zeros, providing more precise control over the matching behavior.
const toRegexRange = require('to-regex-range');
const regex = toRegexRange('10', '299', { relaxZeros: false });
console.log(regex); // Example output: '1[0-9]|2[0-9]{2}'
0
to-regex-range
Returns a regex-compatible range from two numbers, min and max. Validated against more than 1.1 million generated unit tests that run in less than 400ms! Useful for creating regular expressions to validate numbers, ranges, years, etc.
Install
Install with npm:
$ npm install --save to-regex-range
Notes
Validated against 1,117,543 generated unit tests, to provide brute-force verification that the generated regex-ranges are correct.
Usage
var toRegexRange = require('to-regex-range');
var re = new RegExp(toRegexRange('1', '99'));
re.test('50');
Examples
console.log(toRegexRange('111', '555'));
console.log(toRegexRange('5', '5'));
console.log(toRegexRange('5', '6'));
console.log(toRegexRange('51', '229'));
console.log(toRegexRange('29', '51'));
console.log(toRegexRange('1', '100000'));
When the min
is larger than the max
, values will be flipped to create a valid range:
toRegexRange('51', '29');
Heads up!
This library does not support steps (increments) or zero-padding.
History
v1.0
More optimizations! As of v1.0, repeating ranges are now grouped using quantifiers. Processing time is roughly the same, but the generated regex is much smaller, which should result in faster matching.
Key
(for the before/after comparison tables)
range
: the generated range, e.g. toRegexRange(1, 10000000)
stats
: size of the generated string, and processing timeresult
: generated string
Before
Patterns generated before v1.0 changes:
Range | Stats | Result
--- | --- | ---
1..10000000
| 99 B
(11ms 666μs) | ([1-9]|[1-9][0-9]|[1-9][0-9]{2}|[1-9][0-9]{3}|[1-9][0-9]{4}|[1-9][0-9]{5}|[1-9][0-9]{6}|10000000)
1..1000000
| 84 B
(2ms 96μs) | ([1-9]|[1-9][0-9]|[1-9][0-9]{2}|[1-9][0-9]{3}|[1-9][0-9]{4}|[1-9][0-9]{5}|1000000)
1..100000
| 69 B
(1ms 674μs) | ([1-9]|[1-9][0-9]|[1-9][0-9]{2}|[1-9][0-9]{3}|[1-9][0-9]{4}|100000)
1..10000
| 54 B
(2ms 40μs) | ([1-9]|[1-9][0-9]|[1-9][0-9]{2}|[1-9][0-9]{3}|10000)
1..1000
| 39 B
(1ms 263μs) | ([1-9]|[1-9][0-9]|[1-9][0-9]{2}|1000)
1..100
| 24 B
(1ms 905μs) | ([1-9]|[1-9][0-9]|100)
1..10
| 12 B
(383μs) | ([1-9]|10)
1..3
| 9 B
(260μs) | ([1-3])
After
With v1.0 optimizations.
Range | Stats | Result
--- | --- | ---
1..10000000
| 34 B
(11ms 702μs) | ([1-9]|[1-9][0-9]{1,6}|10000000)
1..1000000
| 33 B
(1ms 274μs) | ([1-9]|[1-9][0-9]{1,5}|1000000)
1..100000
| 32 B
(726μs) | ([1-9]|[1-9][0-9]{1,4}|100000)
1..10000
| 31 B
(2ms 432μs) | ([1-9]|[1-9][0-9]{1,3}|10000)
1..1000
| 30 B
(507μs) | ([1-9]|[1-9][0-9]{1,2}|1000)
1..100
| 24 B
(267μs) | ([1-9]|[1-9][0-9]|100)
1..10
| 12 B
(240μs) | ([1-9]|10)
1..3
| 9 B
(665μs) | ([1-3])
Attribution
Inspired by the python lib range-regex.
About
Related projects
- expand-range: Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. See… more | homepage
- fill-range: Fill in a range of numbers or letters, optionally passing an increment or
step
to… more | homepage - micromatch: Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | homepage
- repeat-element: Create an array by repeating the given value n times. | homepage
- repeat-string: Repeat the given string n times. Fastest implementation for repeating a string. | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Building docs
(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)
To generate the readme and API documentation with verb:
$ npm install -g verb verb-generate-readme && verb
Running tests
Install dev dependencies:
$ npm install -d && npm test
Author
Jon Schlinkert
License
Copyright © 2016, Jon Schlinkert.
Released under the MIT license.
This file was generated by verb-generate-readme, v0.2.0, on October 19, 2016.