![Introducing Enhanced Alert Actions and Triage Functionality](https://cdn.sanity.io/images/cgdhsj6q/production/fe71306d515f85de6139b46745ea7180362324f0-2530x946.png?w=800&fit=max&auto=format)
Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
to-regex-range
Advanced tools
Package description
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.
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}'
Similar to to-regex-range, num-regex allows for the creation of regex patterns to match numbers. However, it focuses more on matching numbers in general rather than generating patterns for specific numeric ranges.
Range-regex is another package that offers functionality to create regex patterns for numeric ranges. It provides a different API and might have different performance characteristics or feature sets compared to to-regex-range, offering an alternative approach to solving similar problems.
Readme
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:
$ 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
var toRegexRange = require('to-regex-range');
var 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 July 25, 2018, this library runs 2,783,483 test assertions against generated regex-ranges to provide brute-force verification that results are indeed correct.
Tests run in ~580ms on my MacBook Pro, 2.5 GHz Intel Core i7.
Generated regular expressions are highly 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
var 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).
var source = toRegexRange('15', '95');
//=> 1[5-9]|[2-8][0-9]|9[0-5]
var re = new RegExp('^' + source + '$');
console.log(re.test('14')); //=> false
console.log(re.test('50')); //=> true
console.log(re.test('94')); //=> true
console.log(re.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.
console.log(toRegexRange('-10', '10'));
//=> -[1-9]|-?10|[0-9]
console.log(toRegexRange('-10', '10', {capture: true}));
//=> (-[1-9]|-?10|[0-9])
Type: boolean
Deafault: undefined
Use the regex shorthand for [0-9]
:
console.log(toRegexRange('0', '999999'));
//=> [0-9]|[1-9][0-9]{1,5}
console.log(toRegexRange('0', '999999', {shorthand: true}));
//=> \d|[1-9]\d{1,5}
Type: boolean
Default: true
This option only applies to negative zero-padded ranges. By default, when a negative zero-padded range is defined, the number of leading zeros is relaxed using -0*
.
console.log(toRegexRange('-001', '100'));
//=> -0*1|0{2}[0-9]|0[1-9][0-9]|100
console.log(toRegexRange('-001', '100', {relaxZeros: false}));
//=> -0{2}1|0{2}[0-9]|0[1-9][0-9]|100
Consider the following.
var regex = toRegexRange('-001', '100');
Note that -001
and 100
are both three digits long.
In most zero-padding implementations, only a single leading zero is enough to indicate that zero-padding should be applied. Thus, the leading zeros would be "corrected" on the negative range in the example to -01
, instead of -001
, to make total length of each string no greater than the length of the largest number in the range (in other words, -001
is 4 digits, but 100
is only three digits).
If zeros were not relaxed by default, you might expect the resulting regex of the above pattern to match -001
- given that it's defined that way in the arguments - but it wouldn't. It would, however, match -01
. This gets even more ambiguous with large ranges, like -01
to 1000000
.
Thus, we relax zeros by default to provide a more predictable experience for users.
Range | Result | Compile time |
---|---|---|
toRegexRange('5, 5') | 5 | 20μs |
toRegexRange('5, 6') | 5|6 | 30μs |
toRegexRange('29, 51') | 29|[34][0-9]|5[01] | 341μs |
toRegexRange('31, 877') | 3[1-9]|[4-9][0-9]|[1-7][0-9]{2}|8[0-6][0-9]|87[0-7] | 52μ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] | 37μs |
toRegexRange('-10, 10') | -[1-9]|-?10|[0-9] | 43μs |
toRegexRange('-100, -10') | -1[0-9]|-[2-9][0-9]|-100 | 39μs |
toRegexRange('-100, 100') | -[1-9]|-?[1-9][0-9]|-?100|[0-9] | 54μs |
toRegexRange('001, 100') | 0{2}[1-9]|0[1-9][0-9]|100 | 110μs |
toRegexRange('0010, 1000') | 0{2}1[0-9]|0{2}[2-9][0-9]|0[1-9][0-9]{2}|1000 | 53μs |
toRegexRange('1, 2') | 1|2 | 16μs |
toRegexRange('1, 5') | [1-5] | 21μs |
toRegexRange('1, 10') | [1-9]|10 | 28μs |
toRegexRange('1, 100') | [1-9]|[1-9][0-9]|100 | 50μs |
toRegexRange('1, 1000') | [1-9]|[1-9][0-9]{1,2}|1000 | 68μs |
toRegexRange('1, 10000') | [1-9]|[1-9][0-9]{1,3}|10000 | 69μs |
toRegexRange('1, 100000') | [1-9]|[1-9][0-9]{1,4}|100000 | 54μs |
toRegexRange('1, 1000000') | [1-9]|[1-9][0-9]{1,5}|1000000 | 48μs |
toRegexRange('1, 10000000') | [1-9]|[1-9][0-9]{1,6}|10000000 | 56μs |
Order of arguments
When the min
is larger than the max
, values will be flipped to create a valid range:
toRegexRange('51', '29');
Is effectively flipped to:
toRegexRange('29', '51');
//=> 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.
step
to… more | homepagePull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Commits | Contributor |
---|---|
61 | jonschlinkert |
2 | realityking |
(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:
$ npm install -g verbose/verb#dev verb-generate-readme && verb
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:
$ npm install && npm test
Jon Schlinkert
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.7.0, on July 25, 2018.
FAQs
Pass two numbers, get a regex-compatible source string for matching ranges. Validated against more than 2.78 million test assertions.
The npm package to-regex-range receives a total of 49,301,697 weekly downloads. As such, to-regex-range popularity was classified as popular.
We found that to-regex-range demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.