{"id":14455081,"url":"https://github.com/micromatch/to-regex-range","last_synced_at":"2025-05-15T16:03:18.358Z","repository":{"id":36057311,"uuid":"40355891","full_name":"micromatch/to-regex-range","owner":"micromatch","description":"Pass two numbers, get a regex-compatible source string for matching ranges. Fast compiler, optimized regex, and validated against more than 2.78 million test assertions. Useful for creating regular expressions to validate numbers, ranges, years, etc.","archived":false,"fork":false,"pushed_at":"2024-08-01T09:45:24.000Z","size":75,"stargazers_count":166,"open_issues_count":10,"forks_count":20,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-05-04T14:47:30.199Z","etag":null,"topics":["character-class","expand","integers","javascript","match","matcher","matching","node","nodejs","number","numbers","padding","range","range-expansion","regex","regex-range","regular-expression","repeat","tidelift"],"latest_commit_sha":null,"homepage":"https://github.com/jonschlinkert","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/micromatch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["doowb","jonschlinkert"]}},"created_at":"2015-08-07T10:48:45.000Z","updated_at":"2025-04-07T01:10:47.000Z","dependencies_parsed_at":"2024-09-01T14:02:12.085Z","dependency_job_id":"2bdc6eba-a4fc-4367-9cc1-c026f7c5a4dc","html_url":"https://github.com/micromatch/to-regex-range","commit_stats":{"total_commits":74,"total_committers":3,"mean_commits":"24.666666666666668","dds":0.06756756756756754,"last_synced_commit":"8c9be13043f8a2ad01f26963859fc6ee401960de"},"previous_names":["jonschlinkert/to-regex-range"],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micromatch%2Fto-regex-range","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micromatch%2Fto-regex-range/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micromatch%2Fto-regex-range/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micromatch%2Fto-regex-range/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/micromatch","download_url":"https://codeload.github.com/micromatch/to-regex-range/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253523698,"owners_count":21921815,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["character-class","expand","integers","javascript","match","matcher","matching","node","nodejs","number","numbers","padding","range","range-expansion","regex","regex-range","regular-expression","repeat","tidelift"],"created_at":"2024-09-01T14:01:22.587Z","updated_at":"2025-05-15T16:03:18.331Z","avatar_url":"https://github.com/micromatch.png","language":"JavaScript","funding_links":["https://github.com/sponsors/doowb","https://github.com/sponsors/jonschlinkert","https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=W8YFZ425KND68","https://patreon.com/invite/bxpbvm)!","https://www.patreon.com/jonschlinkert"],"categories":["JavaScript"],"sub_categories":[],"readme":"# to-regex-range [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=W8YFZ425KND68) [![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/micromatch/to-regex-range.svg?style=flat\u0026label=Travis)](https://travis-ci.org/micromatch/to-regex-range)\n\n\u003e Pass two numbers, get a regex-compatible source string for matching ranges. Validated against more than 2.78 million test assertions.\n\nPlease consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n```sh\n$ npm install --save to-regex-range\n```\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eWhat does this do?\u003c/strong\u003e\u003c/summary\u003e\n\n\u003cbr\u003e\n\nThis libary generates the `source` string to be passed to `new RegExp()` for matching a range of numbers.\n\n**Example**\n\n```js\nconst toRegexRange = require('to-regex-range');\nconst regex = new RegExp(toRegexRange('15', '95'));\n```\n\nA 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).\n\n\u003cbr\u003e\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eWhy use this library?\u003c/strong\u003e\u003c/summary\u003e\n\n\u003cbr\u003e\n\n### Convenience\n\nCreating regular expressions for matching numbers gets deceptively complicated pretty fast.\n\nFor example, let's say you need a validation regex for matching part of a user-id, postal code, social security number, tax id, etc:\n\n* regex for matching `1` =\u003e `/1/` (easy enough)\n* regex for matching `1` through `5` =\u003e `/[1-5]/` (not bad...)\n* regex for matching `1` or `5` =\u003e `/(1|5)/` (still easy...)\n* regex for matching `1` through `50` =\u003e `/([1-9]|[1-4][0-9]|50)/` (uh-oh...)\n* regex for matching `1` through `55` =\u003e `/([1-9]|[1-4][0-9]|5[0-5])/` (no prob, I can do this...)\n* regex for matching `1` through `555` =\u003e `/([1-9]|[1-9][0-9]|[1-4][0-9]{2}|5[0-4][0-9]|55[0-5])/` (maybe not...)\n* regex for matching `0001` through `5555` =\u003e `/(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!)\n\nThe 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.\n\n**Learn more**\n\nIf you're interested in learning more about [character classes](http://www.regular-expressions.info/charclass.html) and other regex features, I personally have always found [regular-expressions.info](http://www.regular-expressions.info/charclass.html) to be pretty useful.\n\n### Heavily tested\n\nAs of April 07, 2019, this library runs [\u003e1m test assertions](./test/test.js) against generated regex-ranges to provide brute-force verification that results are correct.\n\nTests run in ~280ms on my MacBook Pro, 2.5 GHz Intel Core i7.\n\n### Optimized\n\nGenerated regular expressions are optimized:\n\n* duplicate sequences and character classes are reduced using quantifiers\n* smart enough to use `?` conditionals when number(s) or range(s) can be positive or negative\n* uses fragment caching to avoid processing the same exact string more than once\n\n\u003cbr\u003e\n\n\u003c/details\u003e\n\n## Usage\n\nAdd this library to your javascript application with the following line of code\n\n```js\nconst toRegexRange = require('to-regex-range');\n```\n\nThe main export is a function that takes two integers: the `min` value and `max` value (formatted as strings or numbers).\n\n```js\nconst source = toRegexRange('15', '95');\n//=\u003e 1[5-9]|[2-8][0-9]|9[0-5]\n\nconst regex = new RegExp(`^${source}$`);\nconsole.log(regex.test('14')); //=\u003e false\nconsole.log(regex.test('50')); //=\u003e true\nconsole.log(regex.test('94')); //=\u003e true\nconsole.log(regex.test('96')); //=\u003e false\n```\n\n## Options\n\n### options.capture\n\n**Type**: `boolean`\n\n**Deafault**: `undefined`\n\nWrap the returned value in parentheses when there is more than one regex condition. Useful when you're dynamically generating ranges.\n\n```js\nconsole.log(toRegexRange('-10', '10'));\n//=\u003e -[1-9]|-?10|[0-9]\n\nconsole.log(toRegexRange('-10', '10', { capture: true }));\n//=\u003e (-[1-9]|-?10|[0-9])\n```\n\n### options.shorthand\n\n**Type**: `boolean`\n\n**Deafault**: `undefined`\n\nUse the regex shorthand for `[0-9]`:\n\n```js\nconsole.log(toRegexRange('0', '999999'));\n//=\u003e [0-9]|[1-9][0-9]{1,5}\n\nconsole.log(toRegexRange('0', '999999', { shorthand: true }));\n//=\u003e \\d|[1-9]\\d{1,5}\n```\n\n### options.relaxZeros\n\n**Type**: `boolean`\n\n**Default**: `true`\n\nThis option relaxes matching for leading zeros when when ranges are zero-padded.\n\n```js\nconst source = toRegexRange('-0010', '0010');\nconst regex = new RegExp(`^${source}$`);\nconsole.log(regex.test('-10')); //=\u003e true\nconsole.log(regex.test('-010')); //=\u003e true\nconsole.log(regex.test('-0010')); //=\u003e true\nconsole.log(regex.test('10')); //=\u003e true\nconsole.log(regex.test('010')); //=\u003e true\nconsole.log(regex.test('0010')); //=\u003e true\n```\n\nWhen `relaxZeros` is false, matching is strict:\n\n```js\nconst source = toRegexRange('-0010', '0010', { relaxZeros: false });\nconst regex = new RegExp(`^${source}$`);\nconsole.log(regex.test('-10')); //=\u003e false\nconsole.log(regex.test('-010')); //=\u003e false\nconsole.log(regex.test('-0010')); //=\u003e true\nconsole.log(regex.test('10')); //=\u003e false\nconsole.log(regex.test('010')); //=\u003e false\nconsole.log(regex.test('0010')); //=\u003e true\n```\n\n## Examples\n\n| **Range**                   | **Result**                                                                      | **Compile time** |\n| ---                         | ---                                                                             | ---              |\n| `toRegexRange(-10, 10)`     | `-[1-9]\\|-?10\\|[0-9]`                                                           | _132μs_          |\n| `toRegexRange(-100, -10)`   | `-1[0-9]\\|-[2-9][0-9]\\|-100`                                                    | _50μs_           |\n| `toRegexRange(-100, 100)`   | `-[1-9]\\|-?[1-9][0-9]\\|-?100\\|[0-9]`                                            | _42μs_           |\n| `toRegexRange(001, 100)`    | `0{0,2}[1-9]\\|0?[1-9][0-9]\\|100`                                                | _109μs_          |\n| `toRegexRange(001, 555)`    | `0{0,2}[1-9]\\|0?[1-9][0-9]\\|[1-4][0-9]{2}\\|5[0-4][0-9]\\|55[0-5]`                | _51μs_           |\n| `toRegexRange(0010, 1000)`  | `0{0,2}1[0-9]\\|0{0,2}[2-9][0-9]\\|0?[1-9][0-9]{2}\\|1000`                         | _31μs_           |\n| `toRegexRange(1, 50)`       | `[1-9]\\|[1-4][0-9]\\|50`                                                         | _24μs_           |\n| `toRegexRange(1, 55)`       | `[1-9]\\|[1-4][0-9]\\|5[0-5]`                                                     | _23μs_           |\n| `toRegexRange(1, 555)`      | `[1-9]\\|[1-9][0-9]\\|[1-4][0-9]{2}\\|5[0-4][0-9]\\|55[0-5]`                        | _30μs_           |\n| `toRegexRange(1, 5555)`     | `[1-9]\\|[1-9][0-9]{1,2}\\|[1-4][0-9]{3}\\|5[0-4][0-9]{2}\\|55[0-4][0-9]\\|555[0-5]` | _43μs_           |\n| `toRegexRange(111, 555)`    | `11[1-9]\\|1[2-9][0-9]\\|[2-4][0-9]{2}\\|5[0-4][0-9]\\|55[0-5]`                     | _38μs_           |\n| `toRegexRange(29, 51)`      | `29\\|[34][0-9]\\|5[01]`                                                          | _24μs_           |\n| `toRegexRange(31, 877)`     | `3[1-9]\\|[4-9][0-9]\\|[1-7][0-9]{2}\\|8[0-6][0-9]\\|87[0-7]`                       | _32μs_           |\n| `toRegexRange(5, 5)`        | `5`                                                                             | _8μs_            |\n| `toRegexRange(5, 6)`        | `5\\|6`                                                                          | _11μs_           |\n| `toRegexRange(1, 2)`        | `1\\|2`                                                                          | _6μs_            |\n| `toRegexRange(1, 5)`        | `[1-5]`                                                                         | _15μs_           |\n| `toRegexRange(1, 10)`       | `[1-9]\\|10`                                                                     | _22μs_           |\n| `toRegexRange(1, 100)`      | `[1-9]\\|[1-9][0-9]\\|100`                                                        | _25μs_           |\n| `toRegexRange(1, 1000)`     | `[1-9]\\|[1-9][0-9]{1,2}\\|1000`                                                  | _31μs_           |\n| `toRegexRange(1, 10000)`    | `[1-9]\\|[1-9][0-9]{1,3}\\|10000`                                                 | _34μs_           |\n| `toRegexRange(1, 100000)`   | `[1-9]\\|[1-9][0-9]{1,4}\\|100000`                                                | _36μs_           |\n| `toRegexRange(1, 1000000)`  | `[1-9]\\|[1-9][0-9]{1,5}\\|1000000`                                               | _42μs_           |\n| `toRegexRange(1, 10000000)` | `[1-9]\\|[1-9][0-9]{1,6}\\|10000000`                                              | _42μs_           |\n\n## Heads up!\n\n**Order of arguments**\n\nWhen the `min` is larger than the `max`, values will be flipped to create a valid range:\n\n```js\ntoRegexRange('51', '29');\n```\n\nIs effectively flipped to:\n\n```js\ntoRegexRange('29', '51');\n//=\u003e 29|[3-4][0-9]|5[0-1]\n```\n\n**Steps / increments**\n\nThis library does not support steps (increments). A pr to add support would be welcome.\n\n## History\n\n### v2.0.0 - 2017-04-21\n\n**New features**\n\nAdds support for zero-padding!\n\n### v1.0.0\n\n**Optimizations**\n\nRepeating 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.\n\n## Attribution\n\nInspired by the python library [range-regex](https://github.com/dimka665/range-regex).\n\n## About\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eContributing\u003c/strong\u003e\u003c/summary\u003e\n\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eRunning Tests\u003c/strong\u003e\u003c/summary\u003e\n\nRunning 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:\n\n```sh\n$ npm install \u0026\u0026 npm test\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eBuilding docs\u003c/strong\u003e\u003c/summary\u003e\n\n_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_\n\nTo generate the readme, run the following command:\n\n```sh\n$ npm install -g verbose/verb#dev verb-generate-readme \u0026\u0026 verb\n```\n\n\u003c/details\u003e\n\n### Related projects\n\nYou might also be interested in these projects:\n\n* [expand-range](https://www.npmjs.com/package/expand-range): Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. Used… [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. Used by micromatch.\")\n* [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`\")\n* [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/micromatch/micromatch \"Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.\")\n* [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.\")\n* [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.\")\n\n### Contributors\n\n| **Commits** | **Contributor** |  \n| --- | --- |  \n| 63 | [jonschlinkert](https://github.com/jonschlinkert) |  \n| 3  | [doowb](https://github.com/doowb) |  \n| 2  | [realityking](https://github.com/realityking) |  \n\n### Author\n\n**Jon Schlinkert**\n\n* [GitHub Profile](https://github.com/jonschlinkert)\n* [Twitter Profile](https://twitter.com/jonschlinkert)\n* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)\n\nPlease consider supporting me on Patreon, or [start your own Patreon page](https://patreon.com/invite/bxpbvm)!\n\n\u003ca href=\"https://www.patreon.com/jonschlinkert\"\u003e\n\u003cimg src=\"https://c5.patreon.com/external/logo/become_a_patron_button@2x.png\" height=\"50\"\u003e\n\u003c/a\u003e\n\n### License\n\nCopyright © 2019, [Jon Schlinkert](https://github.com/jonschlinkert).\nReleased under the [MIT License](LICENSE).\n\n***\n\n_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on April 07, 2019._","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicromatch%2Fto-regex-range","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmicromatch%2Fto-regex-range","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicromatch%2Fto-regex-range/lists"}