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