JSPM

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

Returns a regex-compatible range from two numbers, min and max. 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.

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

Returns a regex-compatible range from two numbers, min and max. 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.

Inspired by the python lib range-regex, it has never been easier to validate numbers and number ranges with regex!

The unit tests generate millions of patterns to provide brute-force validation that the generated regex-ranges are correct.

Install

Install with npm

$ npm i to-regex-range --save

Install with bower

$ bower install to-regex-range --save

Usage

var toRegexRange = require('to-regex-range');

var re = new RegExp(toRegexRange('1', '99'));
re.test('50');
//=> true

Examples

toRegexRange('111', '555');
//=> '11[1-9]|1[2-9]\d|[2-4]\d{2}|5[0-4]\d|55[0-5]'
toRegexRange('5', '5');
//=> '5'
toRegexRange('5', '6');
//=> '[5-6]'
toRegexRange('51', '229');
//=> '5[1-9]|[6-9]\d|1\d{2}|2[0-2]\d'

When the min is larger than the max, a regex logical or is returned:

toRegexRange('51', '29');
//=> '51|29'

Currently this does not support steps (increments) or zero-padding.

  • expand-range: Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. See… more
  • fill-range: Fill in a range of numbers or letters, optionally passing an increment or multiplier to… more
  • micromatch: Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. Just… more
  • repeat-element: Create an array by repeating the given value n times.
  • repeat-string: Repeat the given string n times. Fastest implementation for repeating a string.

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

Author

Jon Schlinkert

License

Copyright © 2015 Jon Schlinkert Released under the MIT license.


This file was generated by verb-cli on August 07, 2015.