1 # extglob [![NPM version](https://img.shields.io/npm/v/extglob.svg?style=flat)](https://www.npmjs.com/package/extglob) [![NPM monthly downloads](https://img.shields.io/npm/dm/extglob.svg?style=flat)](https://npmjs.org/package/extglob) [![NPM total downloads](https://img.shields.io/npm/dt/extglob.svg?style=flat)](https://npmjs.org/package/extglob) [![Linux Build Status](https://img.shields.io/travis/micromatch/extglob.svg?style=flat&label=Travis)](https://travis-ci.org/micromatch/extglob) [![Windows Build Status](https://img.shields.io/appveyor/ci/micromatch/extglob.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/micromatch/extglob)
3 > Extended glob support for JavaScript. Adds (almost) the expressive power of regular expressions to glob patterns.
5 Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
9 Install with [npm](https://www.npmjs.com/):
12 $ npm install --save extglob
15 * Convert an extglob string to a regex-compatible string.
16 * More complete (and correct) support than [minimatch](https://github.com/isaacs/minimatch) (minimatch fails a large percentage of the extglob tests)
17 * Handles [negation patterns](#extglob-patterns)
18 * Handles [nested patterns](#extglob-patterns)
19 * Organized code base, easy to maintain and make changes when edge cases arise
20 * As you can see by the [benchmarks](#benchmarks), extglob doesn't pay with speed for it's completeness, accuracy and quality.
22 **Heads up!**: This library only supports extglobs, to handle full glob patterns and other extended globbing features use [micromatch](https://github.com/jonschlinkert/micromatch) instead.
26 The main export is a function that takes a string and options, and returns an object with the parsed AST and the compiled `.output`, which is a regex-compatible string that can be used for matching.
29 var extglob = require('extglob');
30 console.log(extglob('!(xyz)*.js'));
35 Extended globbing patterns can be defined as follows (as described by the [bash man page](https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html)):
37 | **pattern** | **regex equivalent** | **description** |
39 | `?(pattern-list)` | `(...|...)?` | Matches zero or one occurrence of the given pattern(s) |
40 | `*(pattern-list)` | `(...|...)*` | Matches zero or more occurrences of the given pattern(s) |
41 | `+(pattern-list)` | `(...|...)+` | Matches one or more occurrences of the given pattern(s) |
42 | `@(pattern-list)` | `(...|...)` <sup class="footnote-ref"><a href="#fn1" id="fnref1">[1]</a></sup> | Matches one of the given pattern(s) |
43 | `!(pattern-list)` | N/A | Matches anything except one of the given pattern(s) |
47 ### [extglob](index.js#L36)
49 Convert the given `extglob` pattern into a regex-compatible string. Returns an object with the compiled result and the parsed AST.
53 * `pattern` **{String}**
54 * `options` **{Object}**
55 * `returns` **{String}**
60 var extglob = require('extglob');
61 console.log(extglob('*.!(*a)'));
62 //=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'
65 ### [.match](index.js#L56)
67 Takes an array of strings and an extglob pattern and returns a new array that contains only the strings that match the pattern.
71 * `list` **{Array}**: Array of strings to match
72 * `pattern` **{String}**: Extglob pattern
73 * `options` **{Object}**
74 * `returns` **{Array}**: Returns an array of matches
79 var extglob = require('extglob');
80 console.log(extglob.match(['a.a', 'a.b', 'a.c'], '*.!(*a)'));
84 ### [.isMatch](index.js#L111)
86 Returns true if the specified `string` matches the given extglob `pattern`.
90 * `string` **{String}**: String to match
91 * `pattern` **{String}**: Extglob pattern
92 * `options` **{String}**
93 * `returns` **{Boolean}**
98 var extglob = require('extglob');
100 console.log(extglob.isMatch('a.a', '*.!(*a)'));
102 console.log(extglob.isMatch('a.b', '*.!(*a)'));
106 ### [.contains](index.js#L150)
108 Returns true if the given `string` contains the given pattern. Similar to `.isMatch` but the pattern can match any part of the string.
112 * `str` **{String}**: The string to match.
113 * `pattern` **{String}**: Glob pattern to use for matching.
114 * `options` **{Object}**
115 * `returns` **{Boolean}**: Returns true if the patter matches any part of `str`.
120 var extglob = require('extglob');
121 console.log(extglob.contains('aa/bb/cc', '*b'));
123 console.log(extglob.contains('aa/bb/cc', '*d'));
127 ### [.matcher](index.js#L184)
129 Takes an extglob pattern and returns a matcher function. The returned function takes the string to match as its only argument.
133 * `pattern` **{String}**: Extglob pattern
134 * `options` **{String}**
135 * `returns` **{Boolean}**
140 var extglob = require('extglob');
141 var isMatch = extglob.matcher('*.!(*a)');
143 console.log(isMatch('a.a'));
145 console.log(isMatch('a.b'));
149 ### [.create](index.js#L214)
151 Convert the given `extglob` pattern into a regex-compatible string. Returns an object with the compiled result and the parsed AST.
156 * `options` **{Object}**
157 * `returns` **{String}**
162 var extglob = require('extglob');
163 console.log(extglob.create('*.!(*a)').output);
164 //=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'
167 ### [.capture](index.js#L248)
169 Returns an array of matches captured by `pattern` in `string`, or `null` if the pattern did not match.
173 * `pattern` **{String}**: Glob pattern to use for matching.
174 * `string` **{String}**: String to match
175 * `options` **{Object}**: See available [options](#options) for changing how matches are performed
176 * `returns` **{Boolean}**: Returns an array of captures if the string matches the glob pattern, otherwise `null`.
181 var extglob = require('extglob');
182 extglob.capture(pattern, string[, options]);
184 console.log(extglob.capture('test/*.js', 'test/foo.js'));
186 console.log(extglob.capture('test/*.js', 'foo/bar.css'));
190 ### [.makeRe](index.js#L281)
192 Create a regular expression from the given `pattern` and `options`.
196 * `pattern` **{String}**: The pattern to convert to regex.
197 * `options` **{Object}**
198 * `returns` **{RegExp}**
203 var extglob = require('extglob');
204 var re = extglob.makeRe('*.!(*a)');
206 //=> /^[^\/]*?\.(?![^\/]*?a)[^\/]*?$/
211 Available options are based on the options from Bash (and the option names used in bash).
217 **Default**: `undefined`
219 When enabled, the pattern itself will be returned when no matches are found.
223 Alias for [options.nullglob](#optionsnullglob), included for parity with minimatch.
229 **Default**: `undefined`
231 Functions are memoized based on the given glob patterns and options. Disable memoization by setting `options.cache` to false.
237 **Default**: `undefined`
239 Throw an error is no matches are found.
243 Last run on December 21, 2017
246 # negation-nested (49 bytes)
247 extglob x 2,228,255 ops/sec ±0.98% (89 runs sampled)
248 minimatch x 207,875 ops/sec ±0.61% (91 runs sampled)
250 fastest is extglob (by 1072% avg)
252 # negation-simple (43 bytes)
253 extglob x 2,205,668 ops/sec ±1.00% (91 runs sampled)
254 minimatch x 311,923 ops/sec ±1.25% (91 runs sampled)
256 fastest is extglob (by 707% avg)
258 # range-false (57 bytes)
259 extglob x 2,263,877 ops/sec ±0.40% (94 runs sampled)
260 minimatch x 271,372 ops/sec ±1.02% (91 runs sampled)
262 fastest is extglob (by 834% avg)
264 # range-true (56 bytes)
265 extglob x 2,161,891 ops/sec ±0.41% (92 runs sampled)
266 minimatch x 268,265 ops/sec ±1.17% (91 runs sampled)
268 fastest is extglob (by 806% avg)
270 # star-simple (46 bytes)
271 extglob x 2,211,081 ops/sec ±0.49% (92 runs sampled)
272 minimatch x 343,319 ops/sec ±0.59% (91 runs sampled)
274 fastest is extglob (by 644% avg)
278 ## Differences from Bash
280 This library has complete parity with Bash 4.3 with only a couple of minor differences.
282 * In some cases Bash returns true if the given string "contains" the pattern, whereas this library returns true if the string is an exact match for the pattern. You can relax this by setting `options.contains` to true.
283 * This library is more accurate than Bash and thus does not fail some of the tests that Bash 4.3 still lists as failing in their unit tests
288 <summary><strong>Contributing</strong></summary>
290 Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
295 <summary><strong>Running Tests</strong></summary>
297 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:
300 $ npm install && npm test
305 <summary><strong>Building docs</strong></summary>
307 _(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.)_
309 To generate the readme, run the following command:
312 $ npm install -g verbose/verb#dev verb-generate-readme && verb
319 You might also be interested in these projects:
321 * [braces](https://www.npmjs.com/package/braces): Bash-like brace expansion, implemented in JavaScript. Safer than other brace expansion libs, with complete support… [more](https://github.com/micromatch/braces) | [homepage](https://github.com/micromatch/braces "Bash-like brace expansion, implemented in JavaScript. Safer than other brace expansion libs, with complete support for the Bash 4.3 braces specification, without sacrificing speed.")
322 * [expand-brackets](https://www.npmjs.com/package/expand-brackets): Expand POSIX bracket expressions (character classes) in glob patterns. | [homepage](https://github.com/jonschlinkert/expand-brackets "Expand POSIX bracket expressions (character classes) in glob patterns.")
323 * [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].")
324 * [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`")
325 * [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.")
329 | **Commits** | **Contributor** |
331 | 49 | [jonschlinkert](https://github.com/jonschlinkert) |
332 | 2 | [isiahmeadows](https://github.com/isiahmeadows) |
333 | 1 | [doowb](https://github.com/doowb) |
334 | 1 | [devongovett](https://github.com/devongovett) |
335 | 1 | [mjbvz](https://github.com/mjbvz) |
336 | 1 | [shinnn](https://github.com/shinnn) |
342 * [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert)
343 * [github/jonschlinkert](https://github.com/jonschlinkert)
344 * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
348 Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
349 Released under the [MIT License](LICENSE).
353 _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on December 21, 2017._
355 <hr class="footnotes-sep">
356 <section class="footnotes">
357 <ol class="footnotes-list">
358 <li id="fn1" class="footnote-item">`@` isn "'t a RegEx character." <a href="#fnref1" class="footnote-backref">↩</a>