1# micromatch [![NPM version](https://img.shields.io/npm/v/micromatch.svg?style=flat)](https://www.npmjs.com/package/micromatch) [![NPM monthly downloads](https://img.shields.io/npm/dm/micromatch.svg?style=flat)](https://npmjs.org/package/micromatch) [![NPM total downloads](https://img.shields.io/npm/dt/micromatch.svg?style=flat)](https://npmjs.org/package/micromatch) [![Linux Build Status](https://img.shields.io/travis/micromatch/micromatch.svg?style=flat&label=Travis)](https://travis-ci.org/micromatch/micromatch) [![Windows Build Status](https://img.shields.io/appveyor/ci/micromatch/micromatch.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/micromatch/micromatch) 2 3> Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. 4 5Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. 6 7## Table of Contents 8 9<details> 10<summary><strong>Details</strong></summary> 11 12- [Install](#install) 13- [Quickstart](#quickstart) 14- [Why use micromatch?](#why-use-micromatch) 15 * [Matching features](#matching-features) 16- [Switching to micromatch](#switching-to-micromatch) 17 * [From minimatch](#from-minimatch) 18 * [From multimatch](#from-multimatch) 19- [API](#api) 20- [Options](#options) 21 * [options.basename](#optionsbasename) 22 * [options.bash](#optionsbash) 23 * [options.cache](#optionscache) 24 * [options.dot](#optionsdot) 25 * [options.failglob](#optionsfailglob) 26 * [options.ignore](#optionsignore) 27 * [options.matchBase](#optionsmatchbase) 28 * [options.nobrace](#optionsnobrace) 29 * [options.nocase](#optionsnocase) 30 * [options.nodupes](#optionsnodupes) 31 * [options.noext](#optionsnoext) 32 * [options.nonegate](#optionsnonegate) 33 * [options.noglobstar](#optionsnoglobstar) 34 * [options.nonull](#optionsnonull) 35 * [options.nullglob](#optionsnullglob) 36 * [options.snapdragon](#optionssnapdragon) 37 * [options.sourcemap](#optionssourcemap) 38 * [options.unescape](#optionsunescape) 39 * [options.unixify](#optionsunixify) 40- [Extended globbing](#extended-globbing) 41 * [extglobs](#extglobs) 42 * [braces](#braces) 43 * [regex character classes](#regex-character-classes) 44 * [regex groups](#regex-groups) 45 * [POSIX bracket expressions](#posix-bracket-expressions) 46- [Notes](#notes) 47 * [Bash 4.3 parity](#bash-43-parity) 48 * [Backslashes](#backslashes) 49- [Contributing](#contributing) 50- [Benchmarks](#benchmarks) 51 * [Running benchmarks](#running-benchmarks) 52 * [Latest results](#latest-results) 53- [About](#about) 54 55</details> 56 57## Install 58 59Install with [npm](https://www.npmjs.com/): 60 61```sh 62$ npm install --save micromatch 63``` 64 65## Quickstart 66 67```js 68var mm = require('micromatch'); 69mm(list, patterns[, options]); 70``` 71 72The [main export](#micromatch) takes a list of strings and one or more glob patterns: 73 74```js 75console.log(mm(['foo', 'bar', 'qux'], ['f*', 'b*'])); 76//=> ['foo', 'bar'] 77``` 78 79Use [.isMatch()](#ismatch) to get true/false: 80 81```js 82console.log(mm.isMatch('foo', 'f*')); 83//=> true 84``` 85 86[Switching](#switching-to-micromatch) from minimatch and multimatch is easy! 87 88## Why use micromatch? 89 90> micromatch is a [drop-in replacement](#switching-to-micromatch) for minimatch and multimatch 91 92* Supports all of the same matching features as [minimatch](https://github.com/isaacs/minimatch) and [multimatch](https://github.com/sindresorhus/multimatch) 93* Micromatch uses [snapdragon](https://github.com/jonschlinkert/snapdragon) for parsing and compiling globs, which provides granular control over the entire conversion process in a way that is easy to understand, reason about, and maintain. 94* More consistently accurate matching [than minimatch](https://github.com/yarnpkg/yarn/pull/3339), with more than 36,000 [test assertions](./test) to prove it. 95* More complete support for the Bash 4.3 specification than minimatch and multimatch. In fact, micromatch passes _all of the spec tests_ from bash, including some that bash still fails. 96* [Faster matching](#benchmarks), from a combination of optimized glob patterns, faster algorithms, and regex caching. 97* [Micromatch is safer](https://github.com/micromatch/braces#braces-is-safe), and is not subject to DoS with brace patterns, like minimatch and multimatch. 98* More reliable windows support than minimatch and multimatch. 99 100### Matching features 101 102* Support for multiple glob patterns (no need for wrappers like multimatch) 103* Wildcards (`**`, `*.js`) 104* Negation (`'!a/*.js'`, `'*!(b).js']`) 105* [extglobs](https://github.com/micromatch/extglob) (`+(x|y)`, `!(a|b)`) 106* [POSIX character classes](https://github.com/micromatch/expand-brackets) (`[[:alpha:][:digit:]]`) 107* [brace expansion](https://github.com/micromatch/braces) (`foo/{1..5}.md`, `bar/{a,b,c}.js`) 108* regex character classes (`foo-[1-5].js`) 109* regex logical "or" (`foo/(abc|xyz).js`) 110 111You can mix and match these features to create whatever patterns you need! 112 113## Switching to micromatch 114 115There is one notable difference between micromatch and minimatch in regards to how backslashes are handled. See [the notes about backslashes](#backslashes) for more information. 116 117### From minimatch 118 119Use [mm.isMatch()](#ismatch) instead of `minimatch()`: 120 121```js 122mm.isMatch('foo', 'b*'); 123//=> false 124``` 125 126Use [mm.match()](#match) instead of `minimatch.match()`: 127 128```js 129mm.match(['foo', 'bar'], 'b*'); 130//=> 'bar' 131``` 132 133### From multimatch 134 135Same signature: 136 137```js 138mm(['foo', 'bar', 'baz'], ['f*', '*z']); 139//=> ['foo', 'baz'] 140``` 141 142## API 143 144### [micromatch](index.js#L41) 145 146The main function takes a list of strings and one or more glob patterns to use for matching. 147 148**Params** 149 150* `list` **{Array}**: A list of strings to match 151* `patterns` **{String|Array}**: One or more glob patterns to use for matching. 152* `options` **{Object}**: See available [options](#options) for changing how matches are performed 153* `returns` **{Array}**: Returns an array of matches 154 155**Example** 156 157```js 158var mm = require('micromatch'); 159mm(list, patterns[, options]); 160 161console.log(mm(['a.js', 'a.txt'], ['*.js'])); 162//=> [ 'a.js' ] 163``` 164 165### [.match](index.js#L93) 166 167Similar to the main function, but `pattern` must be a string. 168 169**Params** 170 171* `list` **{Array}**: Array of strings to match 172* `pattern` **{String}**: Glob pattern to use for matching. 173* `options` **{Object}**: See available [options](#options) for changing how matches are performed 174* `returns` **{Array}**: Returns an array of matches 175 176**Example** 177 178```js 179var mm = require('micromatch'); 180mm.match(list, pattern[, options]); 181 182console.log(mm.match(['a.a', 'a.aa', 'a.b', 'a.c'], '*.a')); 183//=> ['a.a', 'a.aa'] 184``` 185 186### [.isMatch](index.js#L154) 187 188Returns true if the specified `string` matches the given glob `pattern`. 189 190**Params** 191 192* `string` **{String}**: String to match 193* `pattern` **{String}**: Glob pattern to use for matching. 194* `options` **{Object}**: See available [options](#options) for changing how matches are performed 195* `returns` **{Boolean}**: Returns true if the string matches the glob pattern. 196 197**Example** 198 199```js 200var mm = require('micromatch'); 201mm.isMatch(string, pattern[, options]); 202 203console.log(mm.isMatch('a.a', '*.a')); 204//=> true 205console.log(mm.isMatch('a.b', '*.a')); 206//=> false 207``` 208 209### [.some](index.js#L192) 210 211Returns true if some of the strings in the given `list` match any of the given glob `patterns`. 212 213**Params** 214 215* `list` **{String|Array}**: The string or array of strings to test. Returns as soon as the first match is found. 216* `patterns` **{String|Array}**: One or more glob patterns to use for matching. 217* `options` **{Object}**: See available [options](#options) for changing how matches are performed 218* `returns` **{Boolean}**: Returns true if any patterns match `str` 219 220**Example** 221 222```js 223var mm = require('micromatch'); 224mm.some(list, patterns[, options]); 225 226console.log(mm.some(['foo.js', 'bar.js'], ['*.js', '!foo.js'])); 227// true 228console.log(mm.some(['foo.js'], ['*.js', '!foo.js'])); 229// false 230``` 231 232### [.every](index.js#L228) 233 234Returns true if every string in the given `list` matches any of the given glob `patterns`. 235 236**Params** 237 238* `list` **{String|Array}**: The string or array of strings to test. 239* `patterns` **{String|Array}**: One or more glob patterns to use for matching. 240* `options` **{Object}**: See available [options](#options) for changing how matches are performed 241* `returns` **{Boolean}**: Returns true if any patterns match `str` 242 243**Example** 244 245```js 246var mm = require('micromatch'); 247mm.every(list, patterns[, options]); 248 249console.log(mm.every('foo.js', ['foo.js'])); 250// true 251console.log(mm.every(['foo.js', 'bar.js'], ['*.js'])); 252// true 253console.log(mm.every(['foo.js', 'bar.js'], ['*.js', '!foo.js'])); 254// false 255console.log(mm.every(['foo.js'], ['*.js', '!foo.js'])); 256// false 257``` 258 259### [.any](index.js#L260) 260 261Returns true if **any** of the given glob `patterns` match the specified `string`. 262 263**Params** 264 265* `str` **{String|Array}**: The string to test. 266* `patterns` **{String|Array}**: One or more glob patterns to use for matching. 267* `options` **{Object}**: See available [options](#options) for changing how matches are performed 268* `returns` **{Boolean}**: Returns true if any patterns match `str` 269 270**Example** 271 272```js 273var mm = require('micromatch'); 274mm.any(string, patterns[, options]); 275 276console.log(mm.any('a.a', ['b.*', '*.a'])); 277//=> true 278console.log(mm.any('a.a', 'b.*')); 279//=> false 280``` 281 282### [.all](index.js#L308) 283 284Returns true if **all** of the given `patterns` match the specified string. 285 286**Params** 287 288* `str` **{String|Array}**: The string to test. 289* `patterns` **{String|Array}**: One or more glob patterns to use for matching. 290* `options` **{Object}**: See available [options](#options) for changing how matches are performed 291* `returns` **{Boolean}**: Returns true if any patterns match `str` 292 293**Example** 294 295```js 296var mm = require('micromatch'); 297mm.all(string, patterns[, options]); 298 299console.log(mm.all('foo.js', ['foo.js'])); 300// true 301 302console.log(mm.all('foo.js', ['*.js', '!foo.js'])); 303// false 304 305console.log(mm.all('foo.js', ['*.js', 'foo.js'])); 306// true 307 308console.log(mm.all('foo.js', ['*.js', 'f*', '*o*', '*o.js'])); 309// true 310``` 311 312### [.not](index.js#L340) 313 314Returns a list of strings that _**do not match any**_ of the given `patterns`. 315 316**Params** 317 318* `list` **{Array}**: Array of strings to match. 319* `patterns` **{String|Array}**: One or more glob pattern to use for matching. 320* `options` **{Object}**: See available [options](#options) for changing how matches are performed 321* `returns` **{Array}**: Returns an array of strings that **do not match** the given patterns. 322 323**Example** 324 325```js 326var mm = require('micromatch'); 327mm.not(list, patterns[, options]); 328 329console.log(mm.not(['a.a', 'b.b', 'c.c'], '*.a')); 330//=> ['b.b', 'c.c'] 331``` 332 333### [.contains](index.js#L376) 334 335Returns true if the given `string` contains the given pattern. Similar to [.isMatch](#isMatch) but the pattern can match any part of the string. 336 337**Params** 338 339* `str` **{String}**: The string to match. 340* `patterns` **{String|Array}**: Glob pattern to use for matching. 341* `options` **{Object}**: See available [options](#options) for changing how matches are performed 342* `returns` **{Boolean}**: Returns true if the patter matches any part of `str`. 343 344**Example** 345 346```js 347var mm = require('micromatch'); 348mm.contains(string, pattern[, options]); 349 350console.log(mm.contains('aa/bb/cc', '*b')); 351//=> true 352console.log(mm.contains('aa/bb/cc', '*d')); 353//=> false 354``` 355 356### [.matchKeys](index.js#L432) 357 358Filter the keys of the given object with the given `glob` pattern and `options`. Does not attempt to match nested keys. If you need this feature, use [glob-object](https://github.com/jonschlinkert/glob-object) instead. 359 360**Params** 361 362* `object` **{Object}**: The object with keys to filter. 363* `patterns` **{String|Array}**: One or more glob patterns to use for matching. 364* `options` **{Object}**: See available [options](#options) for changing how matches are performed 365* `returns` **{Object}**: Returns an object with only keys that match the given patterns. 366 367**Example** 368 369```js 370var mm = require('micromatch'); 371mm.matchKeys(object, patterns[, options]); 372 373var obj = { aa: 'a', ab: 'b', ac: 'c' }; 374console.log(mm.matchKeys(obj, '*b')); 375//=> { ab: 'b' } 376``` 377 378### [.matcher](index.js#L461) 379 380Returns a memoized matcher function from the given glob `pattern` and `options`. The returned function takes a string to match as its only argument and returns true if the string is a match. 381 382**Params** 383 384* `pattern` **{String}**: Glob pattern 385* `options` **{Object}**: See available [options](#options) for changing how matches are performed. 386* `returns` **{Function}**: Returns a matcher function. 387 388**Example** 389 390```js 391var mm = require('micromatch'); 392mm.matcher(pattern[, options]); 393 394var isMatch = mm.matcher('*.!(*a)'); 395console.log(isMatch('a.a')); 396//=> false 397console.log(isMatch('a.b')); 398//=> true 399``` 400 401### [.capture](index.js#L536) 402 403Returns an array of matches captured by `pattern` in `string, or`null` if the pattern did not match. 404 405**Params** 406 407* `pattern` **{String}**: Glob pattern to use for matching. 408* `string` **{String}**: String to match 409* `options` **{Object}**: See available [options](#options) for changing how matches are performed 410* `returns` **{Boolean}**: Returns an array of captures if the string matches the glob pattern, otherwise `null`. 411 412**Example** 413 414```js 415var mm = require('micromatch'); 416mm.capture(pattern, string[, options]); 417 418console.log(mm.capture('test/*.js', 'test/foo.js')); 419//=> ['foo'] 420console.log(mm.capture('test/*.js', 'foo/bar.css')); 421//=> null 422``` 423 424### [.makeRe](index.js#L571) 425 426Create a regular expression from the given glob `pattern`. 427 428**Params** 429 430* `pattern` **{String}**: A glob pattern to convert to regex. 431* `options` **{Object}**: See available [options](#options) for changing how matches are performed. 432* `returns` **{RegExp}**: Returns a regex created from the given pattern. 433 434**Example** 435 436```js 437var mm = require('micromatch'); 438mm.makeRe(pattern[, options]); 439 440console.log(mm.makeRe('*.js')); 441//=> /^(?:(\.[\\\/])?(?!\.)(?=.)[^\/]*?\.js)$/ 442``` 443 444### [.braces](index.js#L618) 445 446Expand the given brace `pattern`. 447 448**Params** 449 450* `pattern` **{String}**: String with brace pattern to expand. 451* `options` **{Object}**: Any [options](#options) to change how expansion is performed. See the [braces](https://github.com/micromatch/braces) library for all available options. 452* `returns` **{Array}** 453 454**Example** 455 456```js 457var mm = require('micromatch'); 458console.log(mm.braces('foo/{a,b}/bar')); 459//=> ['foo/(a|b)/bar'] 460 461console.log(mm.braces('foo/{a,b}/bar', {expand: true})); 462//=> ['foo/(a|b)/bar'] 463``` 464 465### [.create](index.js#L685) 466 467Parses the given glob `pattern` and returns an array of abstract syntax trees (ASTs), with the compiled `output` and optional source `map` on each AST. 468 469**Params** 470 471* `pattern` **{String}**: Glob pattern to parse and compile. 472* `options` **{Object}**: Any [options](#options) to change how parsing and compiling is performed. 473* `returns` **{Object}**: Returns an object with the parsed AST, compiled string and optional source map. 474 475**Example** 476 477```js 478var mm = require('micromatch'); 479mm.create(pattern[, options]); 480 481console.log(mm.create('abc/*.js')); 482// [{ options: { source: 'string', sourcemap: true }, 483// state: {}, 484// compilers: 485// { ... }, 486// output: '(\\.[\\\\\\/])?abc\\/(?!\\.)(?=.)[^\\/]*?\\.js', 487// ast: 488// { type: 'root', 489// errors: [], 490// nodes: 491// [ ... ], 492// dot: false, 493// input: 'abc/*.js' }, 494// parsingErrors: [], 495// map: 496// { version: 3, 497// sources: [ 'string' ], 498// names: [], 499// mappings: 'AAAA,GAAG,EAAC,kBAAC,EAAC,EAAE', 500// sourcesContent: [ 'abc/*.js' ] }, 501// position: { line: 1, column: 28 }, 502// content: {}, 503// files: {}, 504// idx: 6 }] 505``` 506 507### [.parse](index.js#L732) 508 509Parse the given `str` with the given `options`. 510 511**Params** 512 513* `str` **{String}** 514* `options` **{Object}** 515* `returns` **{Object}**: Returns an AST 516 517**Example** 518 519```js 520var mm = require('micromatch'); 521mm.parse(pattern[, options]); 522 523var ast = mm.parse('a/{b,c}/d'); 524console.log(ast); 525// { type: 'root', 526// errors: [], 527// input: 'a/{b,c}/d', 528// nodes: 529// [ { type: 'bos', val: '' }, 530// { type: 'text', val: 'a/' }, 531// { type: 'brace', 532// nodes: 533// [ { type: 'brace.open', val: '{' }, 534// { type: 'text', val: 'b,c' }, 535// { type: 'brace.close', val: '}' } ] }, 536// { type: 'text', val: '/d' }, 537// { type: 'eos', val: '' } ] } 538``` 539 540### [.compile](index.js#L780) 541 542Compile the given `ast` or string with the given `options`. 543 544**Params** 545 546* `ast` **{Object|String}** 547* `options` **{Object}** 548* `returns` **{Object}**: Returns an object that has an `output` property with the compiled string. 549 550**Example** 551 552```js 553var mm = require('micromatch'); 554mm.compile(ast[, options]); 555 556var ast = mm.parse('a/{b,c}/d'); 557console.log(mm.compile(ast)); 558// { options: { source: 'string' }, 559// state: {}, 560// compilers: 561// { eos: [Function], 562// noop: [Function], 563// bos: [Function], 564// brace: [Function], 565// 'brace.open': [Function], 566// text: [Function], 567// 'brace.close': [Function] }, 568// output: [ 'a/(b|c)/d' ], 569// ast: 570// { ... }, 571// parsingErrors: [] } 572``` 573 574### [.clearCache](index.js#L801) 575 576Clear the regex cache. 577 578**Example** 579 580```js 581mm.clearCache(); 582``` 583 584## Options 585 586* [basename](#optionsbasename) 587* [bash](#optionsbash) 588* [cache](#optionscache) 589* [dot](#optionsdot) 590* [failglob](#optionsfailglob) 591* [ignore](#optionsignore) 592* [matchBase](#optionsmatchBase) 593* [nobrace](#optionsnobrace) 594* [nocase](#optionsnocase) 595* [nodupes](#optionsnodupes) 596* [noext](#optionsnoext) 597* [noglobstar](#optionsnoglobstar) 598* [nonull](#optionsnonull) 599* [nullglob](#optionsnullglob) 600* [snapdragon](#optionssnapdragon) 601* [sourcemap](#optionssourcemap) 602* [unescape](#optionsunescape) 603* [unixify](#optionsunixify) 604 605### options.basename 606 607Allow glob patterns without slashes to match a file path based on its basename. Same behavior as [minimatch](https://github.com/isaacs/minimatch) option `matchBase`. 608 609**Type**: `Boolean` 610 611**Default**: `false` 612 613**Example** 614 615```js 616mm(['a/b.js', 'a/c.md'], '*.js'); 617//=> [] 618 619mm(['a/b.js', 'a/c.md'], '*.js', {matchBase: true}); 620//=> ['a/b.js'] 621``` 622 623### options.bash 624 625Enabled by default, this option enforces bash-like behavior with stars immediately following a bracket expression. Bash bracket expressions are similar to regex character classes, but unlike regex, a star following a bracket expression **does not repeat the bracketed characters**. Instead, the star is treated the same as an other star. 626 627**Type**: `Boolean` 628 629**Default**: `true` 630 631**Example** 632 633```js 634var files = ['abc', 'ajz']; 635console.log(mm(files, '[a-c]*')); 636//=> ['abc', 'ajz'] 637 638console.log(mm(files, '[a-c]*', {bash: false})); 639``` 640 641### options.cache 642 643Disable regex and function memoization. 644 645**Type**: `Boolean` 646 647**Default**: `undefined` 648 649### options.dot 650 651Match dotfiles. Same behavior as [minimatch](https://github.com/isaacs/minimatch) option `dot`. 652 653**Type**: `Boolean` 654 655**Default**: `false` 656 657### options.failglob 658 659Similar to the `--failglob` behavior in Bash, throws an error when no matches are found. 660 661**Type**: `Boolean` 662 663**Default**: `undefined` 664 665### options.ignore 666 667String or array of glob patterns to match files to ignore. 668 669**Type**: `String|Array` 670 671**Default**: `undefined` 672 673### options.matchBase 674 675Alias for [options.basename](#options-basename). 676 677### options.nobrace 678 679Disable expansion of brace patterns. Same behavior as [minimatch](https://github.com/isaacs/minimatch) option `nobrace`. 680 681**Type**: `Boolean` 682 683**Default**: `undefined` 684 685See [braces](https://github.com/micromatch/braces) for more information about extended brace expansion. 686 687### options.nocase 688 689Use a case-insensitive regex for matching files. Same behavior as [minimatch](https://github.com/isaacs/minimatch). 690 691**Type**: `Boolean` 692 693**Default**: `undefined` 694 695### options.nodupes 696 697Remove duplicate elements from the result array. 698 699**Type**: `Boolean` 700 701**Default**: `undefined` 702 703**Example** 704 705Example of using the `unescape` and `nodupes` options together: 706 707```js 708mm.match(['a/b/c', 'a/b/c'], 'a/b/c'); 709//=> ['a/b/c', 'a/b/c'] 710 711mm.match(['a/b/c', 'a/b/c'], 'a/b/c', {nodupes: true}); 712//=> ['abc'] 713``` 714 715### options.noext 716 717Disable extglob support, so that extglobs are regarded as literal characters. 718 719**Type**: `Boolean` 720 721**Default**: `undefined` 722 723**Examples** 724 725```js 726mm(['a/z', 'a/b', 'a/!(z)'], 'a/!(z)'); 727//=> ['a/b', 'a/!(z)'] 728 729mm(['a/z', 'a/b', 'a/!(z)'], 'a/!(z)', {noext: true}); 730//=> ['a/!(z)'] (matches only as literal characters) 731``` 732 733### options.nonegate 734 735Disallow negation (`!`) patterns, and treat leading `!` as a literal character to match. 736 737**Type**: `Boolean` 738 739**Default**: `undefined` 740 741### options.noglobstar 742 743Disable matching with globstars (`**`). 744 745**Type**: `Boolean` 746 747**Default**: `undefined` 748 749```js 750mm(['a/b', 'a/b/c', 'a/b/c/d'], 'a/**'); 751//=> ['a/b', 'a/b/c', 'a/b/c/d'] 752 753mm(['a/b', 'a/b/c', 'a/b/c/d'], 'a/**', {noglobstar: true}); 754//=> ['a/b'] 755``` 756 757### options.nonull 758 759Alias for [options.nullglob](#options-nullglob). 760 761### options.nullglob 762 763If `true`, when no matches are found the actual (arrayified) glob pattern is returned instead of an empty array. Same behavior as [minimatch](https://github.com/isaacs/minimatch) option `nonull`. 764 765**Type**: `Boolean` 766 767**Default**: `undefined` 768 769### options.snapdragon 770 771Pass your own instance of [snapdragon](https://github.com/jonschlinkert/snapdragon), to customize parsers or compilers. 772 773**Type**: `Object` 774 775**Default**: `undefined` 776 777### options.sourcemap 778 779Generate a source map by enabling the `sourcemap` option with the `.parse`, `.compile`, or `.create` methods. 780 781_(Note that sourcemaps are currently not enabled for brace patterns)_ 782 783**Examples** 784 785``` js 786var mm = require('micromatch'); 787var pattern = '*(*(of*(a)x)z)'; 788 789var res = mm.create('abc/*.js', {sourcemap: true}); 790console.log(res.map); 791// { version: 3, 792// sources: [ 'string' ], 793// names: [], 794// mappings: 'AAAA,GAAG,EAAC,iBAAC,EAAC,EAAE', 795// sourcesContent: [ 'abc/*.js' ] } 796 797var ast = mm.parse('abc/**/*.js'); 798var res = mm.compile(ast, {sourcemap: true}); 799console.log(res.map); 800// { version: 3, 801// sources: [ 'string' ], 802// names: [], 803// mappings: 'AAAA,GAAG,EAAC,2BAAE,EAAC,iBAAC,EAAC,EAAE', 804// sourcesContent: [ 'abc/**/*.js' ] } 805 806var ast = mm.parse(pattern); 807var res = mm.compile(ast, {sourcemap: true}); 808console.log(res.map); 809// { version: 3, 810// sources: [ 'string' ], 811// names: [], 812// mappings: 'AAAA,CAAE,CAAE,EAAE,CAAE,CAAC,EAAC,CAAC,EAAC,CAAC,EAAC', 813// sourcesContent: [ '*(*(of*(a)x)z)' ] } 814``` 815 816### options.unescape 817 818Remove backslashes from returned matches. 819 820**Type**: `Boolean` 821 822**Default**: `undefined` 823 824**Example** 825 826In this example we want to match a literal `*`: 827 828```js 829mm.match(['abc', 'a\\*c'], 'a\\*c'); 830//=> ['a\\*c'] 831 832mm.match(['abc', 'a\\*c'], 'a\\*c', {unescape: true}); 833//=> ['a*c'] 834``` 835 836### options.unixify 837 838Convert path separators on returned files to posix/unix-style forward slashes. 839 840**Type**: `Boolean` 841 842**Default**: `true` on windows, `false` everywhere else 843 844**Example** 845 846```js 847mm.match(['a\\b\\c'], 'a/**'); 848//=> ['a/b/c'] 849 850mm.match(['a\\b\\c'], {unixify: false}); 851//=> ['a\\b\\c'] 852``` 853 854## Extended globbing 855 856Micromatch also supports extended globbing features. 857 858### extglobs 859 860Extended globbing, as described by the bash man page: 861 862| **pattern** | **regex equivalent** | **description** | 863| --- | --- | --- | 864| `?(pattern)` | `(pattern)?` | Matches zero or one occurrence of the given patterns | 865| `*(pattern)` | `(pattern)*` | Matches zero or more occurrences of the given patterns | 866| `+(pattern)` | `(pattern)+` | Matches one or more occurrences of the given patterns | 867| `@(pattern)` | `(pattern)` <sup>*</sup> | Matches one of the given patterns | 868| `!(pattern)` | N/A (equivalent regex is much more complicated) | Matches anything except one of the given patterns | 869 870<sup><strong>*</strong></sup> Note that `@` isn't a RegEx character. 871 872Powered by [extglob](https://github.com/micromatch/extglob). Visit that library for the full range of options or to report extglob related issues. 873 874### braces 875 876Brace patterns can be used to match specific ranges or sets of characters. For example, the pattern `*/{1..3}/*` would match any of following strings: 877 878``` 879foo/1/bar 880foo/2/bar 881foo/3/bar 882baz/1/qux 883baz/2/qux 884baz/3/qux 885``` 886 887Visit [braces](https://github.com/micromatch/braces) to see the full range of features and options related to brace expansion, or to create brace matching or expansion related issues. 888 889### regex character classes 890 891Given the list: `['a.js', 'b.js', 'c.js', 'd.js', 'E.js']`: 892 893* `[ac].js`: matches both `a` and `c`, returning `['a.js', 'c.js']` 894* `[b-d].js`: matches from `b` to `d`, returning `['b.js', 'c.js', 'd.js']` 895* `[b-d].js`: matches from `b` to `d`, returning `['b.js', 'c.js', 'd.js']` 896* `a/[A-Z].js`: matches and uppercase letter, returning `['a/E.md']` 897 898Learn about [regex character classes](http://www.regular-expressions.info/charclass.html). 899 900### regex groups 901 902Given `['a.js', 'b.js', 'c.js', 'd.js', 'E.js']`: 903 904* `(a|c).js`: would match either `a` or `c`, returning `['a.js', 'c.js']` 905* `(b|d).js`: would match either `b` or `d`, returning `['b.js', 'd.js']` 906* `(b|[A-Z]).js`: would match either `b` or an uppercase letter, returning `['b.js', 'E.js']` 907 908As with regex, parens can be nested, so patterns like `((a|b)|c)/b` will work. Although brace expansion might be friendlier to use, depending on preference. 909 910### POSIX bracket expressions 911 912POSIX brackets are intended to be more user-friendly than regex character classes. This of course is in the eye of the beholder. 913 914**Example** 915 916```js 917mm.isMatch('a1', '[[:alpha:][:digit:]]'); 918//=> true 919 920mm.isMatch('a1', '[[:alpha:][:alpha:]]'); 921//=> false 922``` 923 924See [expand-brackets](https://github.com/jonschlinkert/expand-brackets) for more information about bracket expressions. 925 926*** 927 928## Notes 929 930### Bash 4.3 parity 931 932Whenever possible matching behavior is based on behavior Bash 4.3, which is mostly consistent with minimatch. 933 934However, it's suprising how many edge cases and rabbit holes there are with glob matching, and since there is no real glob specification, and micromatch is more accurate than both Bash and minimatch, there are cases where best-guesses were made for behavior. In a few cases where Bash had no answers, we used wildmatch (used by git) as a fallback. 935 936### Backslashes 937 938There is an important, notable difference between minimatch and micromatch _in regards to how backslashes are handled_ in glob patterns. 939 940* Micromatch exclusively and explicitly reserves backslashes for escaping characters in a glob pattern, even on windows. This is consistent with bash behavior. 941* Minimatch converts all backslashes to forward slashes, which means you can't use backslashes to escape any characters in your glob patterns. 942 943We made this decision for micromatch for a couple of reasons: 944 945* consistency with bash conventions. 946* glob patterns are not filepaths. They are a type of [regular language](https://en.wikipedia.org/wiki/Regular_language) that is converted to a JavaScript regular expression. Thus, when forward slashes are defined in a glob pattern, the resulting regular expression will match windows or POSIX path separators just fine. 947 948**A note about joining paths to globs** 949 950Note that when you pass something like `path.join('foo', '*')` to micromatch, you are creating a filepath and expecting it to still work as a glob pattern. This causes problems on windows, since the `path.sep` is `\\`. 951 952In other words, since `\\` is reserved as an escape character in globs, on windows `path.join('foo', '*')` would result in `foo\\*`, which tells micromatch to match `*` as a literal character. This is the same behavior as bash. 953 954## Contributing 955 956All contributions are welcome! Please read [the contributing guide](.github/contributing.md) to get started. 957 958**Bug reports** 959 960Please create an issue if you encounter a bug or matching behavior that doesn't seem correct. If you find a matching-related issue, please: 961 962* [research existing issues first](../../issues) (open and closed) 963* visit the [GNU Bash documentation](https://www.gnu.org/software/bash/manual/) to see how Bash deals with the pattern 964* visit the [minimatch](https://github.com/isaacs/minimatch) documentation to cross-check expected behavior in node.js 965* if all else fails, since there is no real specification for globs we will probably need to discuss expected behavior and decide how to resolve it. which means any detail you can provide to help with this discussion would be greatly appreciated. 966 967**Platform issues** 968 969It's important to us that micromatch work consistently on all platforms. If you encounter any platform-specific matching or path related issues, please let us know (pull requests are also greatly appreciated). 970 971## Benchmarks 972 973### Running benchmarks 974 975Install dev dependencies: 976 977```bash 978npm i -d && npm run benchmark 979``` 980 981### Latest results 982 983As of February 18, 2018 (longer bars are better): 984 985```sh 986# braces-globstar-large-list (485691 bytes) 987 micromatch ██████████████████████████████████████████████████ (517 ops/sec ±0.49%) 988 minimatch █ (18.92 ops/sec ±0.54%) 989 multimatch █ (18.94 ops/sec ±0.62%) 990 991 micromatch is faster by an avg. of 2,733% 992 993# braces-multiple (3362 bytes) 994 micromatch ██████████████████████████████████████████████████ (33,625 ops/sec ±0.45%) 995 minimatch (2.92 ops/sec ±3.26%) 996 multimatch (2.90 ops/sec ±2.76%) 997 998 micromatch is faster by an avg. of 1,156,935% 999 1000# braces-range (727 bytes) 1001 micromatch █████████████████████████████████████████████████ (155,220 ops/sec ±0.56%) 1002 minimatch ██████ (20,186 ops/sec ±1.27%) 1003 multimatch ██████ (19,809 ops/sec ±0.60%) 1004 1005 micromatch is faster by an avg. of 776% 1006 1007# braces-set (2858 bytes) 1008 micromatch █████████████████████████████████████████████████ (24,354 ops/sec ±0.92%) 1009 minimatch █████ (2,566 ops/sec ±0.56%) 1010 multimatch ████ (2,431 ops/sec ±1.25%) 1011 1012 micromatch is faster by an avg. of 975% 1013 1014# globstar-large-list (485686 bytes) 1015 micromatch █████████████████████████████████████████████████ (504 ops/sec ±0.45%) 1016 minimatch ███ (33.36 ops/sec ±1.08%) 1017 multimatch ███ (33.19 ops/sec ±1.35%) 1018 1019 micromatch is faster by an avg. of 1,514% 1020 1021# globstar-long-list (90647 bytes) 1022 micromatch ██████████████████████████████████████████████████ (2,694 ops/sec ±1.08%) 1023 minimatch ████████████████ (870 ops/sec ±1.09%) 1024 multimatch ████████████████ (862 ops/sec ±0.84%) 1025 1026 micromatch is faster by an avg. of 311% 1027 1028# globstar-short-list (182 bytes) 1029 micromatch ██████████████████████████████████████████████████ (328,921 ops/sec ±1.06%) 1030 minimatch █████████ (64,808 ops/sec ±1.42%) 1031 multimatch ████████ (57,991 ops/sec ±2.11%) 1032 1033 micromatch is faster by an avg. of 536% 1034 1035# no-glob (701 bytes) 1036 micromatch █████████████████████████████████████████████████ (415,935 ops/sec ±0.36%) 1037 minimatch ███████████ (92,730 ops/sec ±1.44%) 1038 multimatch █████████ (81,958 ops/sec ±2.13%) 1039 1040 micromatch is faster by an avg. of 476% 1041 1042# star-basename-long (12339 bytes) 1043 micromatch █████████████████████████████████████████████████ (7,963 ops/sec ±0.36%) 1044 minimatch ███████████████████████████████ (5,072 ops/sec ±0.83%) 1045 multimatch ███████████████████████████████ (5,028 ops/sec ±0.40%) 1046 1047 micromatch is faster by an avg. of 158% 1048 1049# star-basename-short (349 bytes) 1050 micromatch ██████████████████████████████████████████████████ (269,552 ops/sec ±0.70%) 1051 minimatch ██████████████████████ (122,457 ops/sec ±1.39%) 1052 multimatch ████████████████████ (110,788 ops/sec ±1.99%) 1053 1054 micromatch is faster by an avg. of 231% 1055 1056# star-folder-long (19207 bytes) 1057 micromatch █████████████████████████████████████████████████ (3,806 ops/sec ±0.38%) 1058 minimatch ████████████████████████████ (2,204 ops/sec ±0.32%) 1059 multimatch ██████████████████████████ (2,020 ops/sec ±1.07%) 1060 1061 micromatch is faster by an avg. of 180% 1062 1063# star-folder-short (551 bytes) 1064 micromatch ██████████████████████████████████████████████████ (249,077 ops/sec ±0.40%) 1065 minimatch ███████████ (59,431 ops/sec ±1.67%) 1066 multimatch ███████████ (55,569 ops/sec ±1.43%) 1067 1068 micromatch is faster by an avg. of 433% 1069``` 1070 1071## About 1072 1073<details> 1074<summary><strong>Contributing</strong></summary> 1075 1076Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 1077 1078Please read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards. 1079 1080</details> 1081 1082<details> 1083<summary><strong>Running Tests</strong></summary> 1084 1085Running 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: 1086 1087```sh 1088$ npm install && npm test 1089``` 1090 1091</details> 1092 1093<details> 1094<summary><strong>Building docs</strong></summary> 1095 1096_(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.)_ 1097 1098To generate the readme, run the following command: 1099 1100```sh 1101$ npm install -g verbose/verb#dev verb-generate-readme && verb 1102``` 1103 1104</details> 1105 1106### Related projects 1107 1108You might also be interested in these projects: 1109 1110* [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.") 1111* [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.") 1112* [extglob](https://www.npmjs.com/package/extglob): Extended glob support for JavaScript. Adds (almost) the expressive power of regular expressions to glob… [more](https://github.com/micromatch/extglob) | [homepage](https://github.com/micromatch/extglob "Extended glob support for JavaScript. Adds (almost) the expressive power of regular expressions to glob patterns.") 1113* [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`") 1114* [nanomatch](https://www.npmjs.com/package/nanomatch): Fast, minimal glob matcher for node.js. Similar to micromatch, minimatch and multimatch, but complete Bash… [more](https://github.com/micromatch/nanomatch) | [homepage](https://github.com/micromatch/nanomatch "Fast, minimal glob matcher for node.js. Similar to micromatch, minimatch and multimatch, but complete Bash 4.3 wildcard support only (no support for exglobs, posix brackets or braces)") 1115 1116### Contributors 1117 1118| **Commits** | **Contributor** | 1119| --- | --- | 1120| 457 | [jonschlinkert](https://github.com/jonschlinkert) | 1121| 12 | [es128](https://github.com/es128) | 1122| 8 | [doowb](https://github.com/doowb) | 1123| 3 | [paulmillr](https://github.com/paulmillr) | 1124| 2 | [TrySound](https://github.com/TrySound) | 1125| 2 | [MartinKolarik](https://github.com/MartinKolarik) | 1126| 2 | [charlike-old](https://github.com/charlike-old) | 1127| 1 | [amilajack](https://github.com/amilajack) | 1128| 1 | [mrmlnc](https://github.com/mrmlnc) | 1129| 1 | [devongovett](https://github.com/devongovett) | 1130| 1 | [DianeLooney](https://github.com/DianeLooney) | 1131| 1 | [UltCombo](https://github.com/UltCombo) | 1132| 1 | [tomByrer](https://github.com/tomByrer) | 1133| 1 | [fidian](https://github.com/fidian) | 1134 1135### Author 1136 1137**Jon Schlinkert** 1138 1139* [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert) 1140* [github/jonschlinkert](https://github.com/jonschlinkert) 1141* [twitter/jonschlinkert](https://twitter.com/jonschlinkert) 1142 1143### License 1144 1145Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert). 1146Released under the [MIT License](LICENSE). 1147 1148*** 1149 1150_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on February 18, 2018._