# 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)
> Glob matching for javascript/node.js. A replacement and faster alternative to minimatch and multimatch.
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
## Table of Contents
<details>
<summary><strong>Details</strong></summary>
- [Install](#install)
- [Quickstart](#quickstart)
- [Why use micromatch?](#why-use-micromatch)
* [Matching features](#matching-features)
- [Switching to micromatch](#switching-to-micromatch)
* [From minimatch](#from-minimatch)
* [From multimatch](#from-multimatch)
- [API](#api)
- [Options](#options)
- [Options Examples](#options-examples)
* [options.basename](#optionsbasename)
* [options.bash](#optionsbash)
* [options.expandRange](#optionsexpandrange)
* [options.format](#optionsformat)
* [options.ignore](#optionsignore)
* [options.matchBase](#optionsmatchbase)
* [options.noextglob](#optionsnoextglob)
* [options.nonegate](#optionsnonegate)
* [options.noglobstar](#optionsnoglobstar)
* [options.nonull](#optionsnonull)
* [options.nullglob](#optionsnullglob)
* [options.onIgnore](#optionsonignore)
* [options.onMatch](#optionsonmatch)
* [options.onResult](#optionsonresult)
* [options.posixSlashes](#optionsposixslashes)
* [options.unescape](#optionsunescape)
- [Extended globbing](#extended-globbing)
* [Extglobs](#extglobs)
* [Braces](#braces)
* [Regex character classes](#regex-character-classes)
* [Regex groups](#regex-groups)
* [POSIX bracket expressions](#posix-bracket-expressions)
- [Notes](#notes)
* [Bash 4.3 parity](#bash-43-parity)
* [Backslashes](#backslashes)
- [Benchmarks](#benchmarks)
* [Running benchmarks](#running-benchmarks)
* [Latest results](#latest-results)
- [Contributing](#contributing)
- [About](#about)
</details>
## Install
Install with [npm](https://www.npmjs.com/):
```sh
$ npm install --save micromatch
```
## Quickstart
```js
const micromatch = require('micromatch');
// micromatch(list, patterns[, options]);
```
The [main export](#micromatch) takes a list of strings and one or more glob patterns:
```js
console.log(micromatch(['foo', 'bar', 'baz', 'qux'], ['f*', 'b*'])) //=> ['foo', 'bar', 'baz']
console.log(micromatch(['foo', 'bar', 'baz', 'qux'], ['*', '!b*'])) //=> ['foo', 'qux']
```
Use [.isMatch()](#ismatch) to for boolean matching:
```js
console.log(micromatch.isMatch('foo', 'f*')) //=> true
console.log(micromatch.isMatch('foo', ['b*', 'f*'])) //=> true
```
[Switching](#switching-to-micromatch) from minimatch and multimatch is easy!
<br>
## Why use micromatch?
> micromatch is a [replacement](#switching-to-micromatch) for minimatch and multimatch
* Supports all of the same matching features as [minimatch](https://github.com/isaacs/minimatch) and [multimatch](https://github.com/sindresorhus/multimatch)
* More complete support for the Bash 4.3 specification than minimatch and multimatch. Micromatch passes _all of the spec tests_ from bash, including some that bash still fails.
* **Fast & Performant** - Loads in about 5ms and performs [fast matches](#benchmarks).
* **Glob matching** - Using wildcards (`*` and `?`), globstars (`**`) for nested directories
* **[Advanced globbing](#extended-globbing)** - Supports [extglobs](#extglobs), [braces](#braces-1), and [POSIX brackets](#posix-bracket-expressions), and support for escaping special characters with `\` or quotes.
* **Accurate** - Covers more scenarios [than minimatch](https://github.com/yarnpkg/yarn/pull/3339)
* **Well tested** - More than 5,000 [test assertions](./test)
* **Windows support** - More reliable windows support than minimatch and multimatch.
* **[Safe](https://github.com/micromatch/braces#braces-is-safe)** - Micromatch is not subject to DoS with brace patterns like minimatch and multimatch.
### Matching features
* Support for multiple glob patterns (no need for wrappers like multimatch)
* Wildcards (`**`, `*.js`)
* Negation (`'!a/*.js'`, `'*!(b).js']`)
* [extglobs](#extglobs) (`+(x|y)`, `!(a|b)`)
* [POSIX character classes](#posix-bracket-expressions) (`[[:alpha:][:digit:]]`)
* [brace expansion](https://github.com/micromatch/braces) (`foo/{1..5}.md`, `bar/{a,b,c}.js`)
* regex character classes (`foo-[1-5].js`)
* regex logical "or" (`foo/(abc|xyz).js`)
You can mix and match these features to create whatever patterns you need!
## Switching to micromatch
_(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.)_
### From minimatch
Use [micromatch.isMatch()](#ismatch) instead of `minimatch()`:
```js
console.log(micromatch.isMatch('foo', 'b*')); //=> false
```
Use [micromatch.match()](#match) instead of `minimatch.match()`:
```js
console.log(micromatch.match(['foo', 'bar'], 'b*')); //=> 'bar'
```
### From multimatch
Same signature:
```js
console.log(micromatch(['foo', 'bar', 'baz'], ['f*', '*z'])); //=> ['foo', 'baz']
```
## API
**Params**
* `list` **{String|Array<string>}**: List of strings to match.
* `patterns` **{String|Array<string>}**: One or more glob patterns to use for matching.
* `options` **{Object}**: See available [options](#options)
* `returns` **{Array}**: Returns an array of matches
**Example**
```js
const mm = require('micromatch');
// mm(list, patterns[, options]);
console.log(mm(['a.js', 'a.txt'], ['*.js']));
//=> [ 'a.js' ]
```
### [.matcher](index.js#L104)
Returns a 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.
**Params**
* `pattern` **{String}**: Glob pattern
* `options` **{Object}**
* `returns` **{Function}**: Returns a matcher function.
**Example**
```js
const mm = require('micromatch');
// mm.matcher(pattern[, options]);
const isMatch = mm.matcher('*.!(*a)');
console.log(isMatch('a.a')); //=> false
console.log(isMatch('a.b')); //=> true
```
### [.isMatch](index.js#L123)
Returns true if **any** of the given glob `patterns` match the specified `string`.
**Params**
* `str` **{String}**: The string to test.
* `patterns` **{String|Array}**: One or more glob patterns to use for matching.
* `[options]` **{Object}**: See available [options](#options).
* `returns` **{Boolean}**: Returns true if any patterns match `str`
**Example**
```js
const mm = require('micromatch');
// mm.isMatch(string, patterns[, options]);
console.log(mm.isMatch('a.a', ['b.*', '*.a'])); //=> true
console.log(mm.isMatch('a.a', 'b.*')); //=> false
```
### [.not](index.js#L148)
Returns a list of strings that _**do not match any**_ of the given `patterns`.
**Params**
* `list` **{Array}**: Array of strings to match.
* `patterns` **{String|Array}**: One or more glob pattern to use for matching.
* `options` **{Object}**: See available [options](#options) for changing how matches are performed
* `returns` **{Array}**: Returns an array of strings that **do not match** the given patterns.
**Example**
```js
const mm = require('micromatch');
// mm.not(list, patterns[, options]);
console.log(mm.not(['a.a', 'b.b', 'c.c'], '*.a'));
//=> ['b.b', 'c.c']
```
### [.contains](index.js#L188)
Returns true if the given `string` contains the given pattern. Similar to [.isMatch](#isMatch) but the pattern can match any part of the string.
**Params**
* `str` **{String}**: The string to match.
* `p
评论0