You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # to-regex [![NPM version](https://img.shields.io/npm/v/to-regex.svg?style=flat)](https://www.npmjs.com/package/to-regex) [![NPM monthly downloads](https://img.shields.io/npm/dm/to-regex.svg?style=flat)](https://npmjs.org/package/to-regex) [![NPM total downloads](https://img.shields.io/npm/dt/to-regex.svg?style=flat)](https://npmjs.org/package/to-regex) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/to-regex.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/to-regex)
  2. > Generate a regex from a string or array of strings.
  3. Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
  4. - [Install](#install)
  5. - [Usage](#usage)
  6. - [Options](#options)
  7. * [options.contains](#optionscontains)
  8. * [options.negate](#optionsnegate)
  9. * [options.nocase](#optionsnocase)
  10. * [options.flags](#optionsflags)
  11. * [options.cache](#optionscache)
  12. * [options.safe](#optionssafe)
  13. - [About](#about)
  14. * [Related projects](#related-projects)
  15. * [Author](#author)
  16. * [License](#license)
  17. _(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_
  18. ## Install
  19. Install with [npm](https://www.npmjs.com/):
  20. ```sh
  21. $ npm install --save to-regex
  22. ```
  23. ## Usage
  24. ```js
  25. var toRegex = require('to-regex');
  26. console.log(toRegex('foo'));
  27. //=> /^(?:foo)$/
  28. console.log(toRegex('foo', {negate: true}));
  29. //=> /^(?:(?:(?!^(?:foo)$).)*)$/
  30. console.log(toRegex('foo', {contains: true}));
  31. //=> /(?:foo)/
  32. console.log(toRegex(['foo', 'bar'], {negate: true}));
  33. //=> /^(?:(?:(?!^(?:(?:foo)|(?:bar))$).)*)$/
  34. console.log(toRegex(['foo', 'bar'], {negate: true, contains: true}));
  35. //=> /^(?:(?:(?!(?:(?:foo)|(?:bar))).)*)$/
  36. ```
  37. ## Options
  38. ### options.contains
  39. **Type**: `Boolean`
  40. **Default**: `undefined`
  41. Generate a regex that will match any string that _contains_ the given pattern. By default, regex is strict will only return true for exact matches.
  42. ```js
  43. var toRegex = require('to-regex');
  44. console.log(toRegex('foo', {contains: true}));
  45. //=> /(?:foo)/
  46. ```
  47. ### options.negate
  48. **Type**: `Boolean`
  49. **Default**: `undefined`
  50. Create a regex that will match everything except the given pattern.
  51. ```js
  52. var toRegex = require('to-regex');
  53. console.log(toRegex('foo', {negate: true}));
  54. //=> /^(?:(?:(?!^(?:foo)$).)*)$/
  55. ```
  56. ### options.nocase
  57. **Type**: `Boolean`
  58. **Default**: `undefined`
  59. Adds the `i` flag, to enable case-insensitive matching.
  60. ```js
  61. var toRegex = require('to-regex');
  62. console.log(toRegex('foo', {nocase: true}));
  63. //=> /^(?:foo)$/i
  64. ```
  65. Alternatively you can pass the flags you want directly on [options.flags](#options.flags).
  66. ### options.flags
  67. **Type**: `String`
  68. **Default**: `undefined`
  69. Define the flags you want to use on the generated regex.
  70. ```js
  71. var toRegex = require('to-regex');
  72. console.log(toRegex('foo', {flags: 'gm'}));
  73. //=> /^(?:foo)$/gm
  74. console.log(toRegex('foo', {flags: 'gmi', nocase: true})); //<= handles redundancy
  75. //=> /^(?:foo)$/gmi
  76. ```
  77. ### options.cache
  78. **Type**: `Boolean`
  79. **Default**: `true`
  80. Generated regex is cached based on the provided string and options. As a result, runtime compilation only happens once per pattern (as long as options are also the same), which can result in dramatic speed improvements.
  81. This also helps with debugging, since adding options and pattern are added to the generated regex.
  82. **Disable caching**
  83. ```js
  84. toRegex('foo', {cache: false});
  85. ```
  86. ### options.safe
  87. **Type**: `Boolean`
  88. **Default**: `undefined`
  89. Check the generated regular expression with [safe-regex](https://github.com/substack/safe-regex) and throw an error if the regex is potentially unsafe.
  90. **Examples**
  91. ```js
  92. console.log(toRegex('(x+x+)+y'));
  93. //=> /^(?:(x+x+)+y)$/
  94. // The following would throw an error
  95. toRegex('(x+x+)+y', {safe: true});
  96. ```
  97. ## About
  98. <details>
  99. <summary><strong>Contributing</strong></summary>
  100. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  101. </details>
  102. <details>
  103. <summary><strong>Running Tests</strong></summary>
  104. Running 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:
  105. ```sh
  106. $ npm install && npm test
  107. ```
  108. </details>
  109. <details>
  110. <summary><strong>Building docs</strong></summary>
  111. _(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.)_
  112. To generate the readme, run the following command:
  113. ```sh
  114. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  115. ```
  116. </details>
  117. ### Related projects
  118. You might also be interested in these projects:
  119. * [has-glob](https://www.npmjs.com/package/has-glob): Returns `true` if an array has a glob pattern. | [homepage](https://github.com/jonschlinkert/has-glob "Returns `true` if an array has a glob pattern.")
  120. * [is-glob](https://www.npmjs.com/package/is-glob): Returns `true` if the given string looks like a glob pattern or an extglob pattern… [more](https://github.com/jonschlinkert/is-glob) | [homepage](https://github.com/jonschlinkert/is-glob "Returns `true` if the given string looks like a glob pattern or an extglob pattern. This makes it easy to create code that only uses external modules like node-glob when necessary, resulting in much faster code execution and initialization time, and a bet")
  121. * [path-regex](https://www.npmjs.com/package/path-regex): Regular expression for matching the parts of a file path. | [homepage](https://github.com/regexps/path-regex "Regular expression for matching the parts of a file path.")
  122. * [to-regex-range](https://www.npmjs.com/package/to-regex-range): Pass two numbers, get a regex-compatible source string for matching ranges. Validated against more than… [more](https://github.com/micromatch/to-regex-range) | [homepage](https://github.com/micromatch/to-regex-range "Pass two numbers, get a regex-compatible source string for matching ranges. Validated against more than 2.78 million test assertions.")
  123. ### Author
  124. **Jon Schlinkert**
  125. * [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert)
  126. * [github/jonschlinkert](https://github.com/jonschlinkert)
  127. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  128. ### License
  129. Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
  130. Released under the [MIT License](LICENSE).
  131. ***
  132. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on February 24, 2018._