Gathering detailed insights and metrics for to-regex-range
As of June 2024, the npm registry hosts over 2 million packages, making it one of the largest open-source software repositories in the world.
Gathering detailed insights and metrics for to-regex-range
As of June 2024, the npm registry hosts over 2 million packages, making it one of the largest open-source software repositories in the world.
Pass two numbers, get a regex-compatible source string for matching ranges. Fast compiler, optimized regex, and validated against more than 2.78 million test assertions. Useful for creating regular expressions to validate numbers, ranges, years, etc.
npm install to-regex-range
99.3
Supply Chain
100
Quality
78
Maintenance
100
Vulnerability
100
License
162 Stars
79 Commits
19 Forks
6 Watching
1 Branches
10 Contributors
Updated on 22 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
9.1%
11,863,957
Compared to previous day
Last week
5.5%
74,728,668
Compared to previous week
Last month
8.6%
293,505,122
Compared to previous month
Last year
-0.8%
3,116,194,139
Compared to previous year
1
5
Pass two numbers, get a regex-compatible source string for matching ranges. Validated against more than 2.78 million test assertions.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install with npm:
1$ npm install --save to-regex-range
This libary generates the source
string to be passed to new RegExp()
for matching a range of numbers.
Example
1const toRegexRange = require('to-regex-range'); 2const regex = new RegExp(toRegexRange('15', '95'));
A string is returned so that you can do whatever you need with it before passing it to new RegExp()
(like adding ^
or $
boundaries, defining flags, or combining it another string).
Creating regular expressions for matching numbers gets deceptively complicated pretty fast.
For example, let's say you need a validation regex for matching part of a user-id, postal code, social security number, tax id, etc:
1
=> /1/
(easy enough)1
through 5
=> /[1-5]/
(not bad...)1
or 5
=> /(1|5)/
(still easy...)1
through 50
=> /([1-9]|[1-4][0-9]|50)/
(uh-oh...)1
through 55
=> /([1-9]|[1-4][0-9]|5[0-5])/
(no prob, I can do this...)1
through 555
=> /([1-9]|[1-9][0-9]|[1-4][0-9]{2}|5[0-4][0-9]|55[0-5])/
(maybe not...)0001
through 5555
=> /(0{3}[1-9]|0{2}[1-9][0-9]|0[1-9][0-9]{2}|[1-4][0-9]{3}|5[0-4][0-9]{2}|55[0-4][0-9]|555[0-5])/
(okay, I get the point!)The numbers are contrived, but they're also really basic. In the real world you might need to generate a regex on-the-fly for validation.
Learn more
If you're interested in learning more about character classes and other regex features, I personally have always found regular-expressions.info to be pretty useful.
As of April 07, 2019, this library runs >1m test assertions against generated regex-ranges to provide brute-force verification that results are correct.
Tests run in ~280ms on my MacBook Pro, 2.5 GHz Intel Core i7.
Generated regular expressions are optimized:
?
conditionals when number(s) or range(s) can be positive or negativeAdd this library to your javascript application with the following line of code
1const toRegexRange = require('to-regex-range');
The main export is a function that takes two integers: the min
value and max
value (formatted as strings or numbers).
1const source = toRegexRange('15', '95'); 2//=> 1[5-9]|[2-8][0-9]|9[0-5] 3 4const regex = new RegExp(`^${source}$`); 5console.log(regex.test('14')); //=> false 6console.log(regex.test('50')); //=> true 7console.log(regex.test('94')); //=> true 8console.log(regex.test('96')); //=> false
Type: boolean
Deafault: undefined
Wrap the returned value in parentheses when there is more than one regex condition. Useful when you're dynamically generating ranges.
1console.log(toRegexRange('-10', '10')); 2//=> -[1-9]|-?10|[0-9] 3 4console.log(toRegexRange('-10', '10', { capture: true })); 5//=> (-[1-9]|-?10|[0-9])
Type: boolean
Deafault: undefined
Use the regex shorthand for [0-9]
:
1console.log(toRegexRange('0', '999999')); 2//=> [0-9]|[1-9][0-9]{1,5} 3 4console.log(toRegexRange('0', '999999', { shorthand: true })); 5//=> \d|[1-9]\d{1,5}
Type: boolean
Default: true
This option relaxes matching for leading zeros when when ranges are zero-padded.
1const source = toRegexRange('-0010', '0010'); 2const regex = new RegExp(`^${source}$`); 3console.log(regex.test('-10')); //=> true 4console.log(regex.test('-010')); //=> true 5console.log(regex.test('-0010')); //=> true 6console.log(regex.test('10')); //=> true 7console.log(regex.test('010')); //=> true 8console.log(regex.test('0010')); //=> true
When relaxZeros
is false, matching is strict:
1const source = toRegexRange('-0010', '0010', { relaxZeros: false }); 2const regex = new RegExp(`^${source}$`); 3console.log(regex.test('-10')); //=> false 4console.log(regex.test('-010')); //=> false 5console.log(regex.test('-0010')); //=> true 6console.log(regex.test('10')); //=> false 7console.log(regex.test('010')); //=> false 8console.log(regex.test('0010')); //=> true
Range | Result | Compile time |
---|---|---|
toRegexRange(-10, 10) | -[1-9]|-?10|[0-9] | 132μs |
toRegexRange(-100, -10) | -1[0-9]|-[2-9][0-9]|-100 | 50μs |
toRegexRange(-100, 100) | -[1-9]|-?[1-9][0-9]|-?100|[0-9] | 42μs |
toRegexRange(001, 100) | 0{0,2}[1-9]|0?[1-9][0-9]|100 | 109μs |
toRegexRange(001, 555) | 0{0,2}[1-9]|0?[1-9][0-9]|[1-4][0-9]{2}|5[0-4][0-9]|55[0-5] | 51μs |
toRegexRange(0010, 1000) | 0{0,2}1[0-9]|0{0,2}[2-9][0-9]|0?[1-9][0-9]{2}|1000 | 31μs |
toRegexRange(1, 50) | [1-9]|[1-4][0-9]|50 | 24μs |
toRegexRange(1, 55) | [1-9]|[1-4][0-9]|5[0-5] | 23μs |
toRegexRange(1, 555) | [1-9]|[1-9][0-9]|[1-4][0-9]{2}|5[0-4][0-9]|55[0-5] | 30μs |
toRegexRange(1, 5555) | [1-9]|[1-9][0-9]{1,2}|[1-4][0-9]{3}|5[0-4][0-9]{2}|55[0-4][0-9]|555[0-5] | 43μs |
toRegexRange(111, 555) | 11[1-9]|1[2-9][0-9]|[2-4][0-9]{2}|5[0-4][0-9]|55[0-5] | 38μs |
toRegexRange(29, 51) | 29|[34][0-9]|5[01] | 24μs |
toRegexRange(31, 877) | 3[1-9]|[4-9][0-9]|[1-7][0-9]{2}|8[0-6][0-9]|87[0-7] | 32μs |
toRegexRange(5, 5) | 5 | 8μs |
toRegexRange(5, 6) | 5|6 | 11μs |
toRegexRange(1, 2) | 1|2 | 6μs |
toRegexRange(1, 5) | [1-5] | 15μs |
toRegexRange(1, 10) | [1-9]|10 | 22μs |
toRegexRange(1, 100) | [1-9]|[1-9][0-9]|100 | 25μs |
toRegexRange(1, 1000) | [1-9]|[1-9][0-9]{1,2}|1000 | 31μs |
toRegexRange(1, 10000) | [1-9]|[1-9][0-9]{1,3}|10000 | 34μs |
toRegexRange(1, 100000) | [1-9]|[1-9][0-9]{1,4}|100000 | 36μs |
toRegexRange(1, 1000000) | [1-9]|[1-9][0-9]{1,5}|1000000 | 42μs |
toRegexRange(1, 10000000) | [1-9]|[1-9][0-9]{1,6}|10000000 | 42μs |
Order of arguments
When the min
is larger than the max
, values will be flipped to create a valid range:
1toRegexRange('51', '29');
Is effectively flipped to:
1toRegexRange('29', '51'); 2//=> 29|[3-4][0-9]|5[0-1]
Steps / increments
This library does not support steps (increments). A pr to add support would be welcome.
New features
Adds support for zero-padding!
Optimizations
Repeating ranges are now grouped using quantifiers. rocessing time is roughly the same, but the generated regex is much smaller, which should result in faster matching.
Inspired by the python library range-regex.
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
1$ npm install && npm test
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
1$ npm install -g verbose/verb#dev verb-generate-readme && verb
You might also be interested in these projects:
step
to… more | homepageCommits | Contributor |
---|---|
63 | jonschlinkert |
3 | doowb |
2 | realityking |
Jon Schlinkert
Please consider supporting me on Patreon, or start your own Patreon page!
Copyright © 2019, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on April 07, 2019.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
0 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 1
Reason
Found 2/25 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn Morefill-range
Fill in a range of numbers or letters, optionally passing an increment or `step` to use, or create a regex-compatible range with `options.toRegex`
@bluelovers/fill-range
Fill in a range of numbers or letters, optionally passing an increment or `step` to use, or create a regex-compatible range with `options.toRegex`
@bluelovers/to-regex-range2
Pass two numbers, get a regex-compatible source string for matching ranges. Validated against more than 2.78 million test assertions.
extract-range
Extract a string in the given range (e.g. from line X to line Y).