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.

132 lines
4.4 KiB

4 years ago
  1. # regex-not [![NPM version](https://img.shields.io/npm/v/regex-not.svg?style=flat)](https://www.npmjs.com/package/regex-not) [![NPM monthly downloads](https://img.shields.io/npm/dm/regex-not.svg?style=flat)](https://npmjs.org/package/regex-not) [![NPM total downloads](https://img.shields.io/npm/dt/regex-not.svg?style=flat)](https://npmjs.org/package/regex-not) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/regex-not.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/regex-not)
  2. > Create a javascript regular expression for matching everything except for the given string.
  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
  5. Install with [npm](https://www.npmjs.com/):
  6. ```sh
  7. $ npm install --save regex-not
  8. ```
  9. ## Usage
  10. ```js
  11. var not = require('regex-not');
  12. ```
  13. The main export is a function that takes a string an options object.
  14. ```js
  15. not(string[, options]);
  16. ```
  17. **Example**
  18. ```js
  19. var not = require('regex-not');
  20. console.log(not('foo'));
  21. //=> /^(?:(?!^(?:foo)$).)+$/
  22. ```
  23. **Strict matching**
  24. By default, the returned regex is for strictly (not) matching the exact given pattern (in other words, "match this string if it does NOT _exactly equal_ `foo`"):
  25. ```js
  26. var re = not('foo');
  27. console.log(re.test('foo')); //=> false
  28. console.log(re.test('bar')); //=> true
  29. console.log(re.test('foobar')); //=> true
  30. console.log(re.test('barfoo')); //=> true
  31. ```
  32. ### .create
  33. Returns a string to allow you to create your own regex:
  34. ```js
  35. console.log(not.create('foo'));
  36. //=> '(?:(?!^(?:foo)$).)+'
  37. ```
  38. ### Options
  39. **options.contains**
  40. You can relax strict matching by setting `options.contains` to true (in other words, "match this string if it does NOT _contain_ `foo`"):
  41. ```js
  42. var re = not('foo');
  43. console.log(re.test('foo', {contains: true})); //=> false
  44. console.log(re.test('bar', {contains: true})); //=> true
  45. console.log(re.test('foobar', {contains: true})); //=> false
  46. console.log(re.test('barfoo', {contains: true})); //=> false
  47. ```
  48. ## About
  49. <details>
  50. <summary><strong>Contributing</strong></summary>
  51. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  52. </details>
  53. <details>
  54. <summary><strong>Running Tests</strong></summary>
  55. 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:
  56. ```sh
  57. $ npm install && npm test
  58. ```
  59. </details>
  60. <details>
  61. <summary><strong>Building docs</strong></summary>
  62. _(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.)_
  63. To generate the readme, run the following command:
  64. ```sh
  65. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  66. ```
  67. </details>
  68. ### Related projects
  69. You might also be interested in these projects:
  70. * [regex-cache](https://www.npmjs.com/package/regex-cache): Memoize the results of a call to the RegExp constructor, avoiding repetitious runtime compilation of… [more](https://github.com/jonschlinkert/regex-cache) | [homepage](https://github.com/jonschlinkert/regex-cache "Memoize the results of a call to the RegExp constructor, avoiding repetitious runtime compilation of the same string and options, resulting in surprising performance improvements.")
  71. * [to-regex](https://www.npmjs.com/package/to-regex): Generate a regex from a string or array of strings. | [homepage](https://github.com/jonschlinkert/to-regex "Generate a regex from a string or array of strings.")
  72. ### Contributors
  73. | **Commits** | **Contributor** |
  74. | --- | --- |
  75. | 9 | [jonschlinkert](https://github.com/jonschlinkert) |
  76. | 1 | [doowb](https://github.com/doowb) |
  77. | 1 | [EdwardBetts](https://github.com/EdwardBetts) |
  78. ### Author
  79. **Jon Schlinkert**
  80. * [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert)
  81. * [github/jonschlinkert](https://github.com/jonschlinkert)
  82. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  83. ### License
  84. Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
  85. Released under the [MIT License](LICENSE).
  86. ***
  87. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on February 19, 2018._