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.

1149 lines
37 KiB

4 years ago
  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. > Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.
  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. ## Table of Contents
  5. <details>
  6. <summary><strong>Details</strong></summary>
  7. - [Install](#install)
  8. - [Quickstart](#quickstart)
  9. - [Why use micromatch?](#why-use-micromatch)
  10. * [Matching features](#matching-features)
  11. - [Switching to micromatch](#switching-to-micromatch)
  12. * [From minimatch](#from-minimatch)
  13. * [From multimatch](#from-multimatch)
  14. - [API](#api)
  15. - [Options](#options)
  16. * [options.basename](#optionsbasename)
  17. * [options.bash](#optionsbash)
  18. * [options.cache](#optionscache)
  19. * [options.dot](#optionsdot)
  20. * [options.failglob](#optionsfailglob)
  21. * [options.ignore](#optionsignore)
  22. * [options.matchBase](#optionsmatchbase)
  23. * [options.nobrace](#optionsnobrace)
  24. * [options.nocase](#optionsnocase)
  25. * [options.nodupes](#optionsnodupes)
  26. * [options.noext](#optionsnoext)
  27. * [options.nonegate](#optionsnonegate)
  28. * [options.noglobstar](#optionsnoglobstar)
  29. * [options.nonull](#optionsnonull)
  30. * [options.nullglob](#optionsnullglob)
  31. * [options.snapdragon](#optionssnapdragon)
  32. * [options.sourcemap](#optionssourcemap)
  33. * [options.unescape](#optionsunescape)
  34. * [options.unixify](#optionsunixify)
  35. - [Extended globbing](#extended-globbing)
  36. * [extglobs](#extglobs)
  37. * [braces](#braces)
  38. * [regex character classes](#regex-character-classes)
  39. * [regex groups](#regex-groups)
  40. * [POSIX bracket expressions](#posix-bracket-expressions)
  41. - [Notes](#notes)
  42. * [Bash 4.3 parity](#bash-43-parity)
  43. * [Backslashes](#backslashes)
  44. - [Contributing](#contributing)
  45. - [Benchmarks](#benchmarks)
  46. * [Running benchmarks](#running-benchmarks)
  47. * [Latest results](#latest-results)
  48. - [About](#about)
  49. </details>
  50. ## Install
  51. Install with [npm](https://www.npmjs.com/):
  52. ```sh
  53. $ npm install --save micromatch
  54. ```
  55. ## Quickstart
  56. ```js
  57. var mm = require('micromatch');
  58. mm(list, patterns[, options]);
  59. ```
  60. The [main export](#micromatch) takes a list of strings and one or more glob patterns:
  61. ```js
  62. console.log(mm(['foo', 'bar', 'qux'], ['f*', 'b*']));
  63. //=> ['foo', 'bar']
  64. ```
  65. Use [.isMatch()](#ismatch) to get true/false:
  66. ```js
  67. console.log(mm.isMatch('foo', 'f*'));
  68. //=> true
  69. ```
  70. [Switching](#switching-to-micromatch) from minimatch and multimatch is easy!
  71. ## Why use micromatch?
  72. > micromatch is a [drop-in replacement](#switching-to-micromatch) for minimatch and multimatch
  73. * Supports all of the same matching features as [minimatch](https://github.com/isaacs/minimatch) and [multimatch](https://github.com/sindresorhus/multimatch)
  74. * 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.
  75. * More consistently accurate matching [than minimatch](https://github.com/yarnpkg/yarn/pull/3339), with more than 36,000 [test assertions](./test) to prove it.
  76. * 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.
  77. * [Faster matching](#benchmarks), from a combination of optimized glob patterns, faster algorithms, and regex caching.
  78. * [Micromatch is safer](https://github.com/micromatch/braces#braces-is-safe), and is not subject to DoS with brace patterns, like minimatch and multimatch.
  79. * More reliable windows support than minimatch and multimatch.
  80. ### Matching features
  81. * Support for multiple glob patterns (no need for wrappers like multimatch)
  82. * Wildcards (`**`, `*.js`)
  83. * Negation (`'!a/*.js'`, `'*!(b).js']`)
  84. * [extglobs](https://github.com/micromatch/extglob) (`+(x|y)`, `!(a|b)`)
  85. * [POSIX character classes](https://github.com/micromatch/expand-brackets) (`[[:alpha:][:digit:]]`)
  86. * [brace expansion](https://github.com/micromatch/braces) (`foo/{1..5}.md`, `bar/{a,b,c}.js`)
  87. * regex character classes (`foo-[1-5].js`)
  88. * regex logical "or" (`foo/(abc|xyz).js`)
  89. You can mix and match these features to create whatever patterns you need!
  90. ## Switching to micromatch
  91. There is one notable difference between micromatch and minimatch in regards to how backslashes are handled. See [the notes about backslashes](#backslashes) for more information.
  92. ### From minimatch
  93. Use [mm.isMatch()](#ismatch) instead of `minimatch()`:
  94. ```js
  95. mm.isMatch('foo', 'b*');
  96. //=> false
  97. ```
  98. Use [mm.match()](#match) instead of `minimatch.match()`:
  99. ```js
  100. mm.match(['foo', 'bar'], 'b*');
  101. //=> 'bar'
  102. ```
  103. ### From multimatch
  104. Same signature:
  105. ```js
  106. mm(['foo', 'bar', 'baz'], ['f*', '*z']);
  107. //=> ['foo', 'baz']
  108. ```
  109. ## API
  110. ### [micromatch](index.js#L41)
  111. The main function takes a list of strings and one or more glob patterns to use for matching.
  112. **Params**
  113. * `list` **{Array}**: A list of strings to match
  114. * `patterns` **{String|Array}**: One or more glob patterns to use for matching.
  115. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  116. * `returns` **{Array}**: Returns an array of matches
  117. **Example**
  118. ```js
  119. var mm = require('micromatch');
  120. mm(list, patterns[, options]);
  121. console.log(mm(['a.js', 'a.txt'], ['*.js']));
  122. //=> [ 'a.js' ]
  123. ```
  124. ### [.match](index.js#L93)
  125. Similar to the main function, but `pattern` must be a string.
  126. **Params**
  127. * `list` **{Array}**: Array of strings to match
  128. * `pattern` **{String}**: Glob pattern to use for matching.
  129. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  130. * `returns` **{Array}**: Returns an array of matches
  131. **Example**
  132. ```js
  133. var mm = require('micromatch');
  134. mm.match(list, pattern[, options]);
  135. console.log(mm.match(['a.a', 'a.aa', 'a.b', 'a.c'], '*.a'));
  136. //=> ['a.a', 'a.aa']
  137. ```
  138. ### [.isMatch](index.js#L154)
  139. Returns true if the specified `string` matches the given glob `pattern`.
  140. **Params**
  141. * `string` **{String}**: String to match
  142. * `pattern` **{String}**: Glob pattern to use for matching.
  143. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  144. * `returns` **{Boolean}**: Returns true if the string matches the glob pattern.
  145. **Example**
  146. ```js
  147. var mm = require('micromatch');
  148. mm.isMatch(string, pattern[, options]);
  149. console.log(mm.isMatch('a.a', '*.a'));
  150. //=> true
  151. console.log(mm.isMatch('a.b', '*.a'));
  152. //=> false
  153. ```
  154. ### [.some](index.js#L192)
  155. Returns true if some of the strings in the given `list` match any of the given glob `patterns`.
  156. **Params**
  157. * `list` **{String|Array}**: The string or array of strings to test. Returns as soon as the first match is found.
  158. * `patterns` **{String|Array}**: One or more glob patterns to use for matching.
  159. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  160. * `returns` **{Boolean}**: Returns true if any patterns match `str`
  161. **Example**
  162. ```js
  163. var mm = require('micromatch');
  164. mm.some(list, patterns[, options]);
  165. console.log(mm.some(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
  166. // true
  167. console.log(mm.some(['foo.js'], ['*.js', '!foo.js']));
  168. // false
  169. ```
  170. ### [.every](index.js#L228)
  171. Returns true if every string in the given `list` matches any of the given glob `patterns`.
  172. **Params**
  173. * `list` **{String|Array}**: The string or array of strings to test.
  174. * `patterns` **{String|Array}**: One or more glob patterns to use for matching.
  175. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  176. * `returns` **{Boolean}**: Returns true if any patterns match `str`
  177. **Example**
  178. ```js
  179. var mm = require('micromatch');
  180. mm.every(list, patterns[, options]);
  181. console.log(mm.every('foo.js', ['foo.js']));
  182. // true
  183. console.log(mm.every(['foo.js', 'bar.js'], ['*.js']));
  184. // true
  185. console.log(mm.every(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
  186. // false
  187. console.log(mm.every(['foo.js'], ['*.js', '!foo.js']));
  188. // false
  189. ```
  190. ### [.any](index.js#L260)
  191. Returns true if **any** of the given glob `patterns` match the specified `string`.
  192. **Params**
  193. * `str` **{String|Array}**: The string to test.
  194. * `patterns` **{String|Array}**: One or more glob patterns to use for matching.
  195. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  196. * `returns` **{Boolean}**: Returns true if any patterns match `str`
  197. **Example**
  198. ```js
  199. var mm = require('micromatch');
  200. mm.any(string, patterns[, options]);
  201. console.log(mm.any('a.a', ['b.*', '*.a']));
  202. //=> true
  203. console.log(mm.any('a.a', 'b.*'));
  204. //=> false
  205. ```
  206. ### [.all](index.js#L308)
  207. Returns true if **all** of the given `patterns` match the specified string.
  208. **Params**
  209. * `str` **{String|Array}**: The string to test.
  210. * `patterns` **{String|Array}**: One or more glob patterns to use for matching.
  211. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  212. * `returns` **{Boolean}**: Returns true if any patterns match `str`
  213. **Example**
  214. ```js
  215. var mm = require('micromatch');
  216. mm.all(string, patterns[, options]);
  217. console.log(mm.all('foo.js', ['foo.js']));
  218. // true
  219. console.log(mm.all('foo.js', ['*.js', '!foo.js']));
  220. // false
  221. console.log(mm.all('foo.js', ['*.js', 'foo.js']));
  222. // true
  223. console.log(mm.all('foo.js', ['*.js', 'f*', '*o*', '*o.js']));
  224. // true
  225. ```
  226. ### [.not](index.js#L340)
  227. Returns a list of strings that _**do not match any**_ of the given `patterns`.
  228. **Params**
  229. * `list` **{Array}**: Array of strings to match.
  230. * `patterns` **{String|Array}**: One or more glob pattern to use for matching.
  231. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  232. * `returns` **{Array}**: Returns an array of strings that **do not match** the given patterns.
  233. **Example**
  234. ```js
  235. var mm = require('micromatch');
  236. mm.not(list, patterns[, options]);
  237. console.log(mm.not(['a.a', 'b.b', 'c.c'], '*.a'));
  238. //=> ['b.b', 'c.c']
  239. ```
  240. ### [.contains](index.js#L376)
  241. Returns true if the given `string` contains the given pattern. Similar to [.isMatch](#isMatch) but the pattern can match any part of the string.
  242. **Params**
  243. * `str` **{String}**: The string to match.
  244. * `patterns` **{String|Array}**: Glob pattern to use for matching.
  245. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  246. * `returns` **{Boolean}**: Returns true if the patter matches any part of `str`.
  247. **Example**
  248. ```js
  249. var mm = require('micromatch');
  250. mm.contains(string, pattern[, options]);
  251. console.log(mm.contains('aa/bb/cc', '*b'));
  252. //=> true
  253. console.log(mm.contains('aa/bb/cc', '*d'));
  254. //=> false
  255. ```
  256. ### [.matchKeys](index.js#L432)
  257. Filter 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.
  258. **Params**
  259. * `object` **{Object}**: The object with keys to filter.
  260. * `patterns` **{String|Array}**: One or more glob patterns to use for matching.
  261. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  262. * `returns` **{Object}**: Returns an object with only keys that match the given patterns.
  263. **Example**
  264. ```js
  265. var mm = require('micromatch');
  266. mm.matchKeys(object, patterns[, options]);
  267. var obj = { aa: 'a', ab: 'b', ac: 'c' };
  268. console.log(mm.matchKeys(obj, '*b'));
  269. //=> { ab: 'b' }
  270. ```
  271. ### [.matcher](index.js#L461)
  272. Returns 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.
  273. **Params**
  274. * `pattern` **{String}**: Glob pattern
  275. * `options` **{Object}**: See available [options](#options) for changing how matches are performed.
  276. * `returns` **{Function}**: Returns a matcher function.
  277. **Example**
  278. ```js
  279. var mm = require('micromatch');
  280. mm.matcher(pattern[, options]);
  281. var isMatch = mm.matcher('*.!(*a)');
  282. console.log(isMatch('a.a'));
  283. //=> false
  284. console.log(isMatch('a.b'));
  285. //=> true
  286. ```
  287. ### [.capture](index.js#L536)
  288. Returns an array of matches captured by `pattern` in `string, or`null` if the pattern did not match.
  289. **Params**
  290. * `pattern` **{String}**: Glob pattern to use for matching.
  291. * `string` **{String}**: String to match
  292. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  293. * `returns` **{Boolean}**: Returns an array of captures if the string matches the glob pattern, otherwise `null`.
  294. **Example**
  295. ```js
  296. var mm = require('micromatch');
  297. mm.capture(pattern, string[, options]);
  298. console.log(mm.capture('test/*.js', 'test/foo.js'));
  299. //=> ['foo']
  300. console.log(mm.capture('test/*.js', 'foo/bar.css'));
  301. //=> null
  302. ```
  303. ### [.makeRe](index.js#L571)
  304. Create a regular expression from the given glob `pattern`.
  305. **Params**
  306. * `pattern` **{String}**: A glob pattern to convert to regex.
  307. * `options` **{Object}**: See available [options](#options) for changing how matches are performed.
  308. * `returns` **{RegExp}**: Returns a regex created from the given pattern.
  309. **Example**
  310. ```js
  311. var mm = require('micromatch');
  312. mm.makeRe(pattern[, options]);
  313. console.log(mm.makeRe('*.js'));
  314. //=> /^(?:(\.[\\\/])?(?!\.)(?=.)[^\/]*?\.js)$/
  315. ```
  316. ### [.braces](index.js#L618)
  317. Expand the given brace `pattern`.
  318. **Params**
  319. * `pattern` **{String}**: String with brace pattern to expand.
  320. * `options` **{Object}**: Any [options](#options) to change how expansion is performed. See the [braces](https://github.com/micromatch/braces) library for all available options.
  321. * `returns` **{Array}**
  322. **Example**
  323. ```js
  324. var mm = require('micromatch');
  325. console.log(mm.braces('foo/{a,b}/bar'));
  326. //=> ['foo/(a|b)/bar']
  327. console.log(mm.braces('foo/{a,b}/bar', {expand: true}));
  328. //=> ['foo/(a|b)/bar']
  329. ```
  330. ### [.create](index.js#L685)
  331. Parses the given glob `pattern` and returns an array of abstract syntax trees (ASTs), with the compiled `output` and optional source `map` on each AST.
  332. **Params**
  333. * `pattern` **{String}**: Glob pattern to parse and compile.
  334. * `options` **{Object}**: Any [options](#options) to change how parsing and compiling is performed.
  335. * `returns` **{Object}**: Returns an object with the parsed AST, compiled string and optional source map.
  336. **Example**
  337. ```js
  338. var mm = require('micromatch');
  339. mm.create(pattern[, options]);
  340. console.log(mm.create('abc/*.js'));
  341. // [{ options: { source: 'string', sourcemap: true },
  342. // state: {},
  343. // compilers:
  344. // { ... },
  345. // output: '(\\.[\\\\\\/])?abc\\/(?!\\.)(?=.)[^\\/]*?\\.js',
  346. // ast:
  347. // { type: 'root',
  348. // errors: [],
  349. // nodes:
  350. // [ ... ],
  351. // dot: false,
  352. // input: 'abc/*.js' },
  353. // parsingErrors: [],
  354. // map:
  355. // { version: 3,
  356. // sources: [ 'string' ],
  357. // names: [],
  358. // mappings: 'AAAA,GAAG,EAAC,kBAAC,EAAC,EAAE',
  359. // sourcesContent: [ 'abc/*.js' ] },
  360. // position: { line: 1, column: 28 },
  361. // content: {},
  362. // files: {},
  363. // idx: 6 }]
  364. ```
  365. ### [.parse](index.js#L732)
  366. Parse the given `str` with the given `options`.
  367. **Params**
  368. * `str` **{String}**
  369. * `options` **{Object}**
  370. * `returns` **{Object}**: Returns an AST
  371. **Example**
  372. ```js
  373. var mm = require('micromatch');
  374. mm.parse(pattern[, options]);
  375. var ast = mm.parse('a/{b,c}/d');
  376. console.log(ast);
  377. // { type: 'root',
  378. // errors: [],
  379. // input: 'a/{b,c}/d',
  380. // nodes:
  381. // [ { type: 'bos', val: '' },
  382. // { type: 'text', val: 'a/' },
  383. // { type: 'brace',
  384. // nodes:
  385. // [ { type: 'brace.open', val: '{' },
  386. // { type: 'text', val: 'b,c' },
  387. // { type: 'brace.close', val: '}' } ] },
  388. // { type: 'text', val: '/d' },
  389. // { type: 'eos', val: '' } ] }
  390. ```
  391. ### [.compile](index.js#L780)
  392. Compile the given `ast` or string with the given `options`.
  393. **Params**
  394. * `ast` **{Object|String}**
  395. * `options` **{Object}**
  396. * `returns` **{Object}**: Returns an object that has an `output` property with the compiled string.
  397. **Example**
  398. ```js
  399. var mm = require('micromatch');
  400. mm.compile(ast[, options]);
  401. var ast = mm.parse('a/{b,c}/d');
  402. console.log(mm.compile(ast));
  403. // { options: { source: 'string' },
  404. // state: {},
  405. // compilers:
  406. // { eos: [Function],
  407. // noop: [Function],
  408. // bos: [Function],
  409. // brace: [Function],
  410. // 'brace.open': [Function],
  411. // text: [Function],
  412. // 'brace.close': [Function] },
  413. // output: [ 'a/(b|c)/d' ],
  414. // ast:
  415. // { ... },
  416. // parsingErrors: [] }
  417. ```
  418. ### [.clearCache](index.js#L801)
  419. Clear the regex cache.
  420. **Example**
  421. ```js
  422. mm.clearCache();
  423. ```
  424. ## Options
  425. * [basename](#optionsbasename)
  426. * [bash](#optionsbash)
  427. * [cache](#optionscache)
  428. * [dot](#optionsdot)
  429. * [failglob](#optionsfailglob)
  430. * [ignore](#optionsignore)
  431. * [matchBase](#optionsmatchBase)
  432. * [nobrace](#optionsnobrace)
  433. * [nocase](#optionsnocase)
  434. * [nodupes](#optionsnodupes)
  435. * [noext](#optionsnoext)
  436. * [noglobstar](#optionsnoglobstar)
  437. * [nonull](#optionsnonull)
  438. * [nullglob](#optionsnullglob)
  439. * [snapdragon](#optionssnapdragon)
  440. * [sourcemap](#optionssourcemap)
  441. * [unescape](#optionsunescape)
  442. * [unixify](#optionsunixify)
  443. ### options.basename
  444. Allow glob patterns without slashes to match a file path based on its basename. Same behavior as [minimatch](https://github.com/isaacs/minimatch) option `matchBase`.
  445. **Type**: `Boolean`
  446. **Default**: `false`
  447. **Example**
  448. ```js
  449. mm(['a/b.js', 'a/c.md'], '*.js');
  450. //=> []
  451. mm(['a/b.js', 'a/c.md'], '*.js', {matchBase: true});
  452. //=> ['a/b.js']
  453. ```
  454. ### options.bash
  455. Enabled 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.
  456. **Type**: `Boolean`
  457. **Default**: `true`
  458. **Example**
  459. ```js
  460. var files = ['abc', 'ajz'];
  461. console.log(mm(files, '[a-c]*'));
  462. //=> ['abc', 'ajz']
  463. console.log(mm(files, '[a-c]*', {bash: false}));
  464. ```
  465. ### options.cache
  466. Disable regex and function memoization.
  467. **Type**: `Boolean`
  468. **Default**: `undefined`
  469. ### options.dot
  470. Match dotfiles. Same behavior as [minimatch](https://github.com/isaacs/minimatch) option `dot`.
  471. **Type**: `Boolean`
  472. **Default**: `false`
  473. ### options.failglob
  474. Similar to the `--failglob` behavior in Bash, throws an error when no matches are found.
  475. **Type**: `Boolean`
  476. **Default**: `undefined`
  477. ### options.ignore
  478. String or array of glob patterns to match files to ignore.
  479. **Type**: `String|Array`
  480. **Default**: `undefined`
  481. ### options.matchBase
  482. Alias for [options.basename](#options-basename).
  483. ### options.nobrace
  484. Disable expansion of brace patterns. Same behavior as [minimatch](https://github.com/isaacs/minimatch) option `nobrace`.
  485. **Type**: `Boolean`
  486. **Default**: `undefined`
  487. See [braces](https://github.com/micromatch/braces) for more information about extended brace expansion.
  488. ### options.nocase
  489. Use a case-insensitive regex for matching files. Same behavior as [minimatch](https://github.com/isaacs/minimatch).
  490. **Type**: `Boolean`
  491. **Default**: `undefined`
  492. ### options.nodupes
  493. Remove duplicate elements from the result array.
  494. **Type**: `Boolean`
  495. **Default**: `undefined`
  496. **Example**
  497. Example of using the `unescape` and `nodupes` options together:
  498. ```js
  499. mm.match(['a/b/c', 'a/b/c'], 'a/b/c');
  500. //=> ['a/b/c', 'a/b/c']
  501. mm.match(['a/b/c', 'a/b/c'], 'a/b/c', {nodupes: true});
  502. //=> ['abc']
  503. ```
  504. ### options.noext
  505. Disable extglob support, so that extglobs are regarded as literal characters.
  506. **Type**: `Boolean`
  507. **Default**: `undefined`
  508. **Examples**
  509. ```js
  510. mm(['a/z', 'a/b', 'a/!(z)'], 'a/!(z)');
  511. //=> ['a/b', 'a/!(z)']
  512. mm(['a/z', 'a/b', 'a/!(z)'], 'a/!(z)', {noext: true});
  513. //=> ['a/!(z)'] (matches only as literal characters)
  514. ```
  515. ### options.nonegate
  516. Disallow negation (`!`) patterns, and treat leading `!` as a literal character to match.
  517. **Type**: `Boolean`
  518. **Default**: `undefined`
  519. ### options.noglobstar
  520. Disable matching with globstars (`**`).
  521. **Type**: `Boolean`
  522. **Default**: `undefined`
  523. ```js
  524. mm(['a/b', 'a/b/c', 'a/b/c/d'], 'a/**');
  525. //=> ['a/b', 'a/b/c', 'a/b/c/d']
  526. mm(['a/b', 'a/b/c', 'a/b/c/d'], 'a/**', {noglobstar: true});
  527. //=> ['a/b']
  528. ```
  529. ### options.nonull
  530. Alias for [options.nullglob](#options-nullglob).
  531. ### options.nullglob
  532. If `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`.
  533. **Type**: `Boolean`
  534. **Default**: `undefined`
  535. ### options.snapdragon
  536. Pass your own instance of [snapdragon](https://github.com/jonschlinkert/snapdragon), to customize parsers or compilers.
  537. **Type**: `Object`
  538. **Default**: `undefined`
  539. ### options.sourcemap
  540. Generate a source map by enabling the `sourcemap` option with the `.parse`, `.compile`, or `.create` methods.
  541. _(Note that sourcemaps are currently not enabled for brace patterns)_
  542. **Examples**
  543. ``` js
  544. var mm = require('micromatch');
  545. var pattern = '*(*(of*(a)x)z)';
  546. var res = mm.create('abc/*.js', {sourcemap: true});
  547. console.log(res.map);
  548. // { version: 3,
  549. // sources: [ 'string' ],
  550. // names: [],
  551. // mappings: 'AAAA,GAAG,EAAC,iBAAC,EAAC,EAAE',
  552. // sourcesContent: [ 'abc/*.js' ] }
  553. var ast = mm.parse('abc/**/*.js');
  554. var res = mm.compile(ast, {sourcemap: true});
  555. console.log(res.map);
  556. // { version: 3,
  557. // sources: [ 'string' ],
  558. // names: [],
  559. // mappings: 'AAAA,GAAG,EAAC,2BAAE,EAAC,iBAAC,EAAC,EAAE',
  560. // sourcesContent: [ 'abc/**/*.js' ] }
  561. var ast = mm.parse(pattern);
  562. var res = mm.compile(ast, {sourcemap: true});
  563. console.log(res.map);
  564. // { version: 3,
  565. // sources: [ 'string' ],
  566. // names: [],
  567. // mappings: 'AAAA,CAAE,CAAE,EAAE,CAAE,CAAC,EAAC,CAAC,EAAC,CAAC,EAAC',
  568. // sourcesContent: [ '*(*(of*(a)x)z)' ] }
  569. ```
  570. ### options.unescape
  571. Remove backslashes from returned matches.
  572. **Type**: `Boolean`
  573. **Default**: `undefined`
  574. **Example**
  575. In this example we want to match a literal `*`:
  576. ```js
  577. mm.match(['abc', 'a\\*c'], 'a\\*c');
  578. //=> ['a\\*c']
  579. mm.match(['abc', 'a\\*c'], 'a\\*c', {unescape: true});
  580. //=> ['a*c']
  581. ```
  582. ### options.unixify
  583. Convert path separators on returned files to posix/unix-style forward slashes.
  584. **Type**: `Boolean`
  585. **Default**: `true` on windows, `false` everywhere else
  586. **Example**
  587. ```js
  588. mm.match(['a\\b\\c'], 'a/**');
  589. //=> ['a/b/c']
  590. mm.match(['a\\b\\c'], {unixify: false});
  591. //=> ['a\\b\\c']
  592. ```
  593. ## Extended globbing
  594. Micromatch also supports extended globbing features.
  595. ### extglobs
  596. Extended globbing, as described by the bash man page:
  597. | **pattern** | **regex equivalent** | **description** |
  598. | --- | --- | --- |
  599. | `?(pattern)` | `(pattern)?` | Matches zero or one occurrence of the given patterns |
  600. | `*(pattern)` | `(pattern)*` | Matches zero or more occurrences of the given patterns |
  601. | `+(pattern)` | `(pattern)+` | Matches one or more occurrences of the given patterns |
  602. | `@(pattern)` | `(pattern)` <sup>*</sup> | Matches one of the given patterns |
  603. | `!(pattern)` | N/A (equivalent regex is much more complicated) | Matches anything except one of the given patterns |
  604. <sup><strong>*</strong></sup> Note that `@` isn't a RegEx character.
  605. Powered by [extglob](https://github.com/micromatch/extglob). Visit that library for the full range of options or to report extglob related issues.
  606. ### braces
  607. Brace patterns can be used to match specific ranges or sets of characters. For example, the pattern `*/{1..3}/*` would match any of following strings:
  608. ```
  609. foo/1/bar
  610. foo/2/bar
  611. foo/3/bar
  612. baz/1/qux
  613. baz/2/qux
  614. baz/3/qux
  615. ```
  616. Visit [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.
  617. ### regex character classes
  618. Given the list: `['a.js', 'b.js', 'c.js', 'd.js', 'E.js']`:
  619. * `[ac].js`: matches both `a` and `c`, returning `['a.js', 'c.js']`
  620. * `[b-d].js`: matches from `b` to `d`, returning `['b.js', 'c.js', 'd.js']`
  621. * `[b-d].js`: matches from `b` to `d`, returning `['b.js', 'c.js', 'd.js']`
  622. * `a/[A-Z].js`: matches and uppercase letter, returning `['a/E.md']`
  623. Learn about [regex character classes](http://www.regular-expressions.info/charclass.html).
  624. ### regex groups
  625. Given `['a.js', 'b.js', 'c.js', 'd.js', 'E.js']`:
  626. * `(a|c).js`: would match either `a` or `c`, returning `['a.js', 'c.js']`
  627. * `(b|d).js`: would match either `b` or `d`, returning `['b.js', 'd.js']`
  628. * `(b|[A-Z]).js`: would match either `b` or an uppercase letter, returning `['b.js', 'E.js']`
  629. As 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.
  630. ### POSIX bracket expressions
  631. POSIX brackets are intended to be more user-friendly than regex character classes. This of course is in the eye of the beholder.
  632. **Example**
  633. ```js
  634. mm.isMatch('a1', '[[:alpha:][:digit:]]');
  635. //=> true
  636. mm.isMatch('a1', '[[:alpha:][:alpha:]]');
  637. //=> false
  638. ```
  639. See [expand-brackets](https://github.com/jonschlinkert/expand-brackets) for more information about bracket expressions.
  640. ***
  641. ## Notes
  642. ### Bash 4.3 parity
  643. Whenever possible matching behavior is based on behavior Bash 4.3, which is mostly consistent with minimatch.
  644. However, 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.
  645. ### Backslashes
  646. There is an important, notable difference between minimatch and micromatch _in regards to how backslashes are handled_ in glob patterns.
  647. * Micromatch exclusively and explicitly reserves backslashes for escaping characters in a glob pattern, even on windows. This is consistent with bash behavior.
  648. * Minimatch converts all backslashes to forward slashes, which means you can't use backslashes to escape any characters in your glob patterns.
  649. We made this decision for micromatch for a couple of reasons:
  650. * consistency with bash conventions.
  651. * 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.
  652. **A note about joining paths to globs**
  653. Note 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 `\\`.
  654. In 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.
  655. ## Contributing
  656. All contributions are welcome! Please read [the contributing guide](.github/contributing.md) to get started.
  657. **Bug reports**
  658. Please create an issue if you encounter a bug or matching behavior that doesn't seem correct. If you find a matching-related issue, please:
  659. * [research existing issues first](../../issues) (open and closed)
  660. * visit the [GNU Bash documentation](https://www.gnu.org/software/bash/manual/) to see how Bash deals with the pattern
  661. * visit the [minimatch](https://github.com/isaacs/minimatch) documentation to cross-check expected behavior in node.js
  662. * 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.
  663. **Platform issues**
  664. It'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).
  665. ## Benchmarks
  666. ### Running benchmarks
  667. Install dev dependencies:
  668. ```bash
  669. npm i -d && npm run benchmark
  670. ```
  671. ### Latest results
  672. As of February 18, 2018 (longer bars are better):
  673. ```sh
  674. # braces-globstar-large-list (485691 bytes)
  675. micromatch ██████████████████████████████████████████████████ (517 ops/sec ±0.49%)
  676. minimatch █ (18.92 ops/sec ±0.54%)
  677. multimatch █ (18.94 ops/sec ±0.62%)
  678. micromatch is faster by an avg. of 2,733%
  679. # braces-multiple (3362 bytes)
  680. micromatch ██████████████████████████████████████████████████ (33,625 ops/sec ±0.45%)
  681. minimatch (2.92 ops/sec ±3.26%)
  682. multimatch (2.90 ops/sec ±2.76%)
  683. micromatch is faster by an avg. of 1,156,935%
  684. # braces-range (727 bytes)
  685. micromatch █████████████████████████████████████████████████ (155,220 ops/sec ±0.56%)
  686. minimatch ██████ (20,186 ops/sec ±1.27%)
  687. multimatch ██████ (19,809 ops/sec ±0.60%)
  688. micromatch is faster by an avg. of 776%
  689. # braces-set (2858 bytes)
  690. micromatch █████████████████████████████████████████████████ (24,354 ops/sec ±0.92%)
  691. minimatch █████ (2,566 ops/sec ±0.56%)
  692. multimatch ████ (2,431 ops/sec ±1.25%)
  693. micromatch is faster by an avg. of 975%
  694. # globstar-large-list (485686 bytes)
  695. micromatch █████████████████████████████████████████████████ (504 ops/sec ±0.45%)
  696. minimatch ███ (33.36 ops/sec ±1.08%)
  697. multimatch ███ (33.19 ops/sec ±1.35%)
  698. micromatch is faster by an avg. of 1,514%
  699. # globstar-long-list (90647 bytes)
  700. micromatch ██████████████████████████████████████████████████ (2,694 ops/sec ±1.08%)
  701. minimatch ████████████████ (870 ops/sec ±1.09%)
  702. multimatch ████████████████ (862 ops/sec ±0.84%)
  703. micromatch is faster by an avg. of 311%
  704. # globstar-short-list (182 bytes)
  705. micromatch ██████████████████████████████████████████████████ (328,921 ops/sec ±1.06%)
  706. minimatch █████████ (64,808 ops/sec ±1.42%)
  707. multimatch ████████ (57,991 ops/sec ±2.11%)
  708. micromatch is faster by an avg. of 536%
  709. # no-glob (701 bytes)
  710. micromatch █████████████████████████████████████████████████ (415,935 ops/sec ±0.36%)
  711. minimatch ███████████ (92,730 ops/sec ±1.44%)
  712. multimatch █████████ (81,958 ops/sec ±2.13%)
  713. micromatch is faster by an avg. of 476%
  714. # star-basename-long (12339 bytes)
  715. micromatch █████████████████████████████████████████████████ (7,963 ops/sec ±0.36%)
  716. minimatch ███████████████████████████████ (5,072 ops/sec ±0.83%)
  717. multimatch ███████████████████████████████ (5,028 ops/sec ±0.40%)
  718. micromatch is faster by an avg. of 158%
  719. # star-basename-short (349 bytes)
  720. micromatch ██████████████████████████████████████████████████ (269,552 ops/sec ±0.70%)
  721. minimatch ██████████████████████ (122,457 ops/sec ±1.39%)
  722. multimatch ████████████████████ (110,788 ops/sec ±1.99%)
  723. micromatch is faster by an avg. of 231%
  724. # star-folder-long (19207 bytes)
  725. micromatch █████████████████████████████████████████████████ (3,806 ops/sec ±0.38%)
  726. minimatch ████████████████████████████ (2,204 ops/sec ±0.32%)
  727. multimatch ██████████████████████████ (2,020 ops/sec ±1.07%)
  728. micromatch is faster by an avg. of 180%
  729. # star-folder-short (551 bytes)
  730. micromatch ██████████████████████████████████████████████████ (249,077 ops/sec ±0.40%)
  731. minimatch ███████████ (59,431 ops/sec ±1.67%)
  732. multimatch ███████████ (55,569 ops/sec ±1.43%)
  733. micromatch is faster by an avg. of 433%
  734. ```
  735. ## About
  736. <details>
  737. <summary><strong>Contributing</strong></summary>
  738. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  739. Please read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards.
  740. </details>
  741. <details>
  742. <summary><strong>Running Tests</strong></summary>
  743. 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:
  744. ```sh
  745. $ npm install && npm test
  746. ```
  747. </details>
  748. <details>
  749. <summary><strong>Building docs</strong></summary>
  750. _(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.)_
  751. To generate the readme, run the following command:
  752. ```sh
  753. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  754. ```
  755. </details>
  756. ### Related projects
  757. You might also be interested in these projects:
  758. * [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.")
  759. * [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.")
  760. * [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.")
  761. * [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`")
  762. * [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)")
  763. ### Contributors
  764. | **Commits** | **Contributor** |
  765. | --- | --- |
  766. | 457 | [jonschlinkert](https://github.com/jonschlinkert) |
  767. | 12 | [es128](https://github.com/es128) |
  768. | 8 | [doowb](https://github.com/doowb) |
  769. | 3 | [paulmillr](https://github.com/paulmillr) |
  770. | 2 | [TrySound](https://github.com/TrySound) |
  771. | 2 | [MartinKolarik](https://github.com/MartinKolarik) |
  772. | 2 | [charlike-old](https://github.com/charlike-old) |
  773. | 1 | [amilajack](https://github.com/amilajack) |
  774. | 1 | [mrmlnc](https://github.com/mrmlnc) |
  775. | 1 | [devongovett](https://github.com/devongovett) |
  776. | 1 | [DianeLooney](https://github.com/DianeLooney) |
  777. | 1 | [UltCombo](https://github.com/UltCombo) |
  778. | 1 | [tomByrer](https://github.com/tomByrer) |
  779. | 1 | [fidian](https://github.com/fidian) |
  780. ### Author
  781. **Jon Schlinkert**
  782. * [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert)
  783. * [github/jonschlinkert](https://github.com/jonschlinkert)
  784. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  785. ### License
  786. Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
  787. Released under the [MIT License](LICENSE).
  788. ***
  789. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on February 18, 2018._