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.

75 lines
3.2 KiB

4 years ago
  1. # Glob To Regular Expression
  2. [![Build Status](https://travis-ci.org/fitzgen/glob-to-regexp.png?branch=master)](https://travis-ci.org/fitzgen/glob-to-regexp)
  3. Turn a \*-wildcard style glob (`"*.min.js"`) into a regular expression
  4. (`/^.*\.min\.js$/`)!
  5. To match bash-like globs, eg. `?` for any single-character match, `[a-z]` for
  6. character ranges, and `{*.html, *.js}` for multiple alternatives, call with
  7. `{ extended: true }`.
  8. To obey [globstars `**`](https://github.com/isaacs/node-glob#glob-primer) rules set option `{globstar: true}`.
  9. NOTE: This changes the behavior of `*` when `globstar` is `true` as shown below:
  10. When `{globstar: true}`: `/foo/**` will match any string that starts with `/foo/`
  11. like `/foo/index.htm`, `/foo/bar/baz.txt`, etc. Also, `/foo/**/*.txt` will match
  12. any string that starts with `/foo/` and ends with `.txt` like `/foo/bar.txt`,
  13. `/foo/bar/baz.txt`, etc.
  14. Whereas `/foo/*` (single `*`, not a globstar) will match strings that start with
  15. `/foo/` like `/foo/index.htm`, `/foo/baz.txt` but will not match strings that
  16. contain a `/` to the right like `/foo/bar/baz.txt`, `/foo/bar/baz/qux.dat`, etc.
  17. Set flags on the resulting `RegExp` object by adding the `flags` property to the option object, eg `{ flags: "i" }` for ignoring case.
  18. ## Install
  19. npm install glob-to-regexp
  20. ## Usage
  21. ```js
  22. var globToRegExp = require('glob-to-regexp');
  23. var re = globToRegExp("p*uck");
  24. re.test("pot luck"); // true
  25. re.test("pluck"); // true
  26. re.test("puck"); // true
  27. re = globToRegExp("*.min.js");
  28. re.test("http://example.com/jquery.min.js"); // true
  29. re.test("http://example.com/jquery.min.js.map"); // false
  30. re = globToRegExp("*/www/*.js");
  31. re.test("http://example.com/www/app.js"); // true
  32. re.test("http://example.com/www/lib/factory-proxy-model-observer.js"); // true
  33. // Extended globs
  34. re = globToRegExp("*/www/{*.js,*.html}", { extended: true });
  35. re.test("http://example.com/www/app.js"); // true
  36. re.test("http://example.com/www/index.html"); // true
  37. ```
  38. ## License
  39. Copyright (c) 2013, Nick Fitzgerald
  40. All rights reserved.
  41. Redistribution and use in source and binary forms, with or without modification,
  42. are permitted provided that the following conditions are met:
  43. * Redistributions of source code must retain the above copyright notice, this
  44. list of conditions and the following disclaimer.
  45. * Redistributions in binary form must reproduce the above copyright notice, this
  46. list of conditions and the following disclaimer in the documentation and/or
  47. other materials provided with the distribution.
  48. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  49. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  50. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  51. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  52. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  53. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  55. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  56. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  57. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.