JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 70291855
  • Score
    100M100P100Q241112F
  • License MIT

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.

Package Exports

  • to-regex-range

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (to-regex-range) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

to-regex-range NPM version NPM monthly downloads NPM total downloads Linux Build Status

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');
//=> true

Examples

console.log(toRegexRange('111', '555'));
//=> 11[1-9]|1[2-9][0-9]|[2-4][0-9]{2}|5[0-4][0-9]|55[0-5]

console.log(toRegexRange('5', '5'));
//=> 5

console.log(toRegexRange('5', '6'));
//=> [5-6]

console.log(toRegexRange('51', '229'));
//=> 5[1-9]|[6-9][0-9]|1[0-9]{2}|2[0-2][0-9]

console.log(toRegexRange('29', '51'));
//=> 29|[3-4][0-9]|5[0-1]

console.log(toRegexRange('1', '100000'));
//=> [1-9]|[1-9][0-9]{1,4}|100000

When the min is larger than the max, values will be flipped to create a valid range:

toRegexRange('51', '29');
//=> 29|[3-4][0-9]|5[0-1]

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 time
  • result: 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

  • 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.