1 | # to-regex-range [![NPM version](https://img.shields.io/npm/v/to-regex-range.svg?style=flat)](https://www.npmjs.com/package/to-regex-range) [![NPM monthly downloads](https://img.shields.io/npm/dm/to-regex-range.svg?style=flat)](https://npmjs.org/package/to-regex-range) [![NPM total downloads](https://img.shields.io/npm/dt/to-regex-range.svg?style=flat)](https://npmjs.org/package/to-regex-range) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/to-regex-range.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/to-regex-range)
|
2 |
|
3 | > 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.
|
4 |
|
5 | ## Install
|
6 |
|
7 | Install with [npm](https://www.npmjs.com/):
|
8 |
|
9 | ```sh
|
10 | $ npm install --save to-regex-range
|
11 | ```
|
12 |
|
13 | ## Notes
|
14 |
|
15 | Validated against [1,117,543 generated unit tests](./test/test.js), to provide brute-force verification that the generated regex-ranges are correct.
|
16 |
|
17 | ## Usage
|
18 |
|
19 | ```js
|
20 | var toRegexRange = require('to-regex-range');
|
21 |
|
22 | var re = new RegExp(toRegexRange('1', '99'));
|
23 | re.test('50');
|
24 | //=> true
|
25 | ```
|
26 |
|
27 | **Examples**
|
28 |
|
29 | ```js
|
30 | console.log(toRegexRange('111', '555'));
|
31 | //=> 11[1-9]|1[2-9][0-9]|[2-4][0-9]{2}|5[0-4][0-9]|55[0-5]
|
32 |
|
33 | console.log(toRegexRange('5', '5'));
|
34 | //=> 5
|
35 |
|
36 | console.log(toRegexRange('5', '6'));
|
37 | //=> [5-6]
|
38 |
|
39 | console.log(toRegexRange('51', '229'));
|
40 | //=> 5[1-9]|[6-9][0-9]|1[0-9]{2}|2[0-2][0-9]
|
41 |
|
42 | console.log(toRegexRange('29', '51'));
|
43 | //=> 29|[3-4][0-9]|5[0-1]
|
44 |
|
45 | console.log(toRegexRange('1', '100000'));
|
46 | //=> [1-9]|[1-9][0-9]{1,4}|100000
|
47 | ```
|
48 |
|
49 | When the `min` is larger than the `max`, values will be flipped to create a valid range:
|
50 |
|
51 | ```js
|
52 | toRegexRange('51', '29');
|
53 | //=> 29|[3-4][0-9]|5[0-1]
|
54 | ```
|
55 |
|
56 | **Heads up!**
|
57 |
|
58 | This library does not support steps (increments) or zero-padding.
|
59 |
|
60 | ## History
|
61 |
|
62 | ### v1.0
|
63 |
|
64 | 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.
|
65 |
|
66 | **Key**
|
67 |
|
68 | _(for the before/after comparison tables)_
|
69 |
|
70 | * `range`: the generated range, e.g. `toRegexRange(1, 10000000)`
|
71 | * `stats`: size of the generated string, and processing time
|
72 | * `result`: generated string
|
73 |
|
74 | #### Before
|
75 |
|
76 | Patterns generated before v1.0 changes:
|
77 |
|
78 | **Range** | **Stats** | **Result**
|
79 |
|
80 | --- | --- | ---
|
81 | `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)`
|
82 | `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)`
|
83 | `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)`
|
84 | `1..10000` | `54 B` (2ms 40μs) | `([1-9]|[1-9][0-9]|[1-9][0-9]{2}|[1-9][0-9]{3}|10000)`
|
85 | `1..1000` | `39 B` (1ms 263μs) | `([1-9]|[1-9][0-9]|[1-9][0-9]{2}|1000)`
|
86 | `1..100` | `24 B` (1ms 905μs) | `([1-9]|[1-9][0-9]|100)`
|
87 | `1..10` | `12 B` (383μs) | `([1-9]|10)`
|
88 | `1..3` | `9 B` (260μs) | `([1-3])`
|
89 |
|
90 | #### After
|
91 |
|
92 | With v1.0 optimizations.
|
93 |
|
94 | **Range** | **Stats** | **Result**
|
95 |
|
96 | --- | --- | ---
|
97 | `1..10000000` | `34 B` (11ms 702μs) | `([1-9]|[1-9][0-9]{1,6}|10000000)`
|
98 | `1..1000000` | `33 B` (1ms 274μs) | `([1-9]|[1-9][0-9]{1,5}|1000000)`
|
99 | `1..100000` | `32 B` (726μs) | `([1-9]|[1-9][0-9]{1,4}|100000)`
|
100 | `1..10000` | `31 B` (2ms 432μs) | `([1-9]|[1-9][0-9]{1,3}|10000)`
|
101 | `1..1000` | `30 B` (507μs) | `([1-9]|[1-9][0-9]{1,2}|1000)`
|
102 | `1..100` | `24 B` (267μs) | `([1-9]|[1-9][0-9]|100)`
|
103 | `1..10` | `12 B` (240μs) | `([1-9]|10)`
|
104 | `1..3` | `9 B` (665μs) | `([1-3])`
|
105 |
|
106 | ## Attribution
|
107 |
|
108 | Inspired by the python lib [range-regex](https://github.com/dimka665/range-regex).
|
109 |
|
110 | ## About
|
111 |
|
112 | ### Related projects
|
113 |
|
114 | * [expand-range](https://www.npmjs.com/package/expand-range): Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. See… [more](https://github.com/jonschlinkert/expand-range) | [homepage](https://github.com/jonschlinkert/expand-range "Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. See the benchmarks. Used by micromatch.")
|
115 | * [fill-range](https://www.npmjs.com/package/fill-range): Fill in a range of numbers or letters, optionally passing an increment or `step` to… [more](https://github.com/jonschlinkert/fill-range) | [homepage](https://github.com/jonschlinkert/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`")
|
116 | * [micromatch](https://www.npmjs.com/package/micromatch): Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | [homepage](https://github.com/jonschlinkert/micromatch "Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.")
|
117 | * [repeat-element](https://www.npmjs.com/package/repeat-element): Create an array by repeating the given value n times. | [homepage](https://github.com/jonschlinkert/repeat-element "Create an array by repeating the given value n times.")
|
118 | * [repeat-string](https://www.npmjs.com/package/repeat-string): Repeat the given string n times. Fastest implementation for repeating a string. | [homepage](https://github.com/jonschlinkert/repeat-string "Repeat the given string n times. Fastest implementation for repeating a string.")
|
119 |
|
120 | ### Contributing
|
121 |
|
122 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
|
123 |
|
124 | ### Building docs
|
125 |
|
126 | _(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_
|
127 |
|
128 | To generate the readme and API documentation with [verb](https://github.com/verbose/verb):
|
129 |
|
130 | ```sh
|
131 | $ npm install -g verb verb-generate-readme && verb
|
132 | ```
|
133 |
|
134 | ### Running tests
|
135 |
|
136 | Install dev dependencies:
|
137 |
|
138 | ```sh
|
139 | $ npm install -d && npm test
|
140 | ```
|
141 |
|
142 | ### Author
|
143 |
|
144 | **Jon Schlinkert**
|
145 |
|
146 | * [github/jonschlinkert](https://github.com/jonschlinkert)
|
147 | * [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
|
148 |
|
149 | ### License
|
150 |
|
151 | Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert).
|
152 | Released under the [MIT license](https://github.com/jonschlinkert/to-regex-range/blob/master/LICENSE).
|
153 |
|
154 | ***
|
155 |
|
156 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.2.0, on October 19, 2016._ |
\ | No newline at end of file |