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, with 850,000 generated unit tests to validate it's accuracy! Useful for creating regular expressions to validate numbers, ranges, years, etc. Returns a string, allowing the returned value to be used in regular expressions generated by other libraries.
Install
Install with npm:
$ npm install --save to-regex-range
Bower
Install with bower
$ bower install to-regex-range --save
Notes
Inspired by the python lib range-regex, it has never been easier to validate numbers and number ranges with regex!
The unit tests generate 850,000 patterns to provide brute-force validation 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
toRegexRange('111', '555');
toRegexRange('5', '5');
toRegexRange('5', '6');
toRegexRange('51', '229');
When the min
is larger than the max
, a regex logical or
is returned:
toRegexRange('51', '29');
Currently this does not support steps (increments) or zero-padding.
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 multiplier 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.1.30, on September 14, 2016.