var mm = require('micromatch');
mm(list, patterns[, options]);
The main export takes a list of strings and one or more glob patterns:
console.log(mm(['foo', 'bar', 'qux'], ['f*', 'b*'])); // ['foo', 'bar']
Use .isMatch to get true/false:
console.log(mm.isMatch('foo', 'f*')); // true
Switching from minimatch and multimatch is easy:
- use mm.isMatch() instead of
minimatch()
- mm.match() is the same as
minimatch.match()
- mm() is the same as
multimatch()
Heads up!
There is one notable difference between micromatch and minimatch in regards to how backslashes are handled. See the notes about backslashes for more information.
micromatch is a drop-in replacement for minimatch and multimatch
Speed and accuracy
Micromatch uses [snapdragon][] for parsing and compiling globs, which results in:
-
Granular control over the entire conversion process in a way that is easy to understand, reason about, and customize.
-
Faster matching, from a combination of optimized glob patterns and (optional) caching.
-
Much greater accuracy than minimatch. In fact, nanomatch passes all of the spec tests from bash, including some that bash still fails. However, since there is no real specification for globs, if you encounter a pattern that yields unexpected match results after researching previous issues, please let us know.
-
Micromatch is safer, and is not subject to DoS with brace patterns, like minimatch and multimatch.
-
faster matching
-
More accurate, with more than 36,000 unit tests - and thousands more patterns tested - to prove it. (minimatch and multimatch fail a great number of the tests).
-
More complete support for the Bash 4.3 specification than minimatch and multimatch
-
More reliable windows support than minimatch and multimatch
-
Supports all of the same matching features as minimatch and multimatch
-
Micromatch uses real parsers and compilers. Although edge cases are inevitable, micromatch is better equipped to handle them.
- Native support for multiple glob patterns (no need for wrappers like multimatch)
- Basic wildcard support (
**/*
,a/b/*.js
, or['a/*.js', '*b.js']
) - Negation support (
['!a/*.js', '*!(b).js']
) - [extglob][] support (
+(x|y)
,!(a|b)
, etc) - POSIX character class support (
**/[[:alpha:][:digit:]]/
) - brace expansion support (
a/b-{1..5}.md
,one/{two,three}/four.md
) - regex character classes (
a/b/baz-[1-5].js
) - regex logical or (
a/b/(abc|xyz).js
)
You can mix and match these features to create whatever patterns you need!
Example
mm(['a/b.js', 'a/b.md'], 'a/*.!(js)');
//=> ['a/b.md']
Use mm.isMatch()
instead of minimatch()
:
mm.isMatch('foo', 'b*');
//=> false
Use mm.match()
instead of minimatch.match()
:
mm.match(['foo', 'bar'], 'b*');
//=> 'bar'
Same signature:
mm(['foo', 'bar', 'baz'], ['f*', '*z']);
//=> ['foo', 'baz']
{%= apidocs("index.js") %}
- basename
- bash
- cache
- dot
- failglob
- ignore
- matchBase
- nobrace
- nocase
- nodupes
- noext
- noglobstar
- nonull
- nullglob
- snapdragon
- sourcemap
- unescape
- unixify
Allow glob patterns without slashes to match a file path based on its basename. Same behavior as [minimatch][] option matchBase
.
Type: Boolean
Default: false
Example
mm(['a/b.js', 'a/c.md'], '*.js');
//=> []
mm(['a/b.js', 'a/c.md'], '*.js', {matchBase: true});
//=> ['a/b.js']
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.
Type: Boolean
Default: true
Example
var files = ['abc', 'ajz'];
console.log(mm(files, '[a-c]*'));
//=> ['abc', 'ajz']
console.log(mm(files, '[a-c]*', {bash: false}));
Disable regex and function memoization.
Type: Boolean
Default: undefined
Match dotfiles. Same behavior as [minimatch][] option dot
.
Type: Boolean
Default: false
Similar to the --failglob
behavior in Bash, throws an error when no matches are found.
Type: Boolean
Default: undefined
String or array of glob patterns to match files to ignore.
Type: String|Array
Default: undefined
Alias for options.basename.
Disable expansion of brace patterns. Same behavior as [minimatch][] option nobrace
.
Type: Boolean
Default: undefined
See braces for more information about extended brace expansion.
Use a case-insensitive regex for matching files. Same behavior as [minimatch][].
Type: Boolean
Default: undefined
Remove duplicate elements from the result array.
Type: Boolean
Default: undefined
Example
Example of using the unescape
and nodupes
options together:
mm.match(['a/b/c', 'a/b/c'], 'a/b/c');
//=> ['a/b/c', 'a/b/c']
mm.match(['a/b/c', 'a/b/c'], 'a/b/c', {nodupes: true});
//=> ['abc']
Disable extglob support, so that extglobs are regarded as literal characters.
Type: Boolean
Default: undefined
Examples
mm(['a/z', 'a/b', 'a/!(z)'], 'a/!(z)');
//=> ['a/b', 'a/!(z)']
mm(['a/z', 'a/b', 'a/!(z)'], 'a/!(z)', {noext: true});
//=> ['a/!(z)'] (matches only as literal characters)
Disallow negation (!
) patterns, and treat leading !
as a literal character to match.
Type: Boolean
Default: undefined
Disable matching with globstars (**
).
Type: Boolean
Default: undefined
mm(['a/b', 'a/b/c', 'a/b/c/d'], 'a/**');
//=> ['a/b', 'a/b/c', 'a/b/c/d']
mm(['a/b', 'a/b/c', 'a/b/c/d'], 'a/**', {noglobstar: true});
//=> ['a/b']
Alias for options.nullglob.
If true
, when no matches are found the actual (arrayified) glob pattern is returned instead of an empty array. Same behavior as [minimatch][] option nonull
.
Type: Boolean
Default: undefined
Pass your own instance of [snapdragon][], to customize parsers or compilers.
Type: Object
Default: undefined
Generate a source map by enabling the sourcemap
option with the .parse
, .compile
, or .create
methods.
(Note that sourcemaps are currently not enabled for brace patterns)
Examples
var mm = require('micromatch');
var pattern = '*(*(of*(a)x)z)';
var res = mm.create('abc/*.js', {sourcemap: true});
console.log(res.map);
// { version: 3,
// sources: [ 'string' ],
// names: [],
// mappings: 'AAAA,GAAG,EAAC,iBAAC,EAAC,EAAE',
// sourcesContent: [ 'abc/*.js' ] }
var ast = mm.parse('abc/**/*.js');
var res = mm.compile(ast, {sourcemap: true});
console.log(res.map);
// { version: 3,
// sources: [ 'string' ],
// names: [],
// mappings: 'AAAA,GAAG,EAAC,2BAAE,EAAC,iBAAC,EAAC,EAAE',
// sourcesContent: [ 'abc/**/*.js' ] }
var ast = mm.parse(pattern);
var res = mm.compile(ast, {sourcemap: true});
console.log(res.map);
// { version: 3,
// sources: [ 'string' ],
// names: [],
// mappings: 'AAAA,CAAE,CAAE,EAAE,CAAE,CAAC,EAAC,CAAC,EAAC,CAAC,EAAC',
// sourcesContent: [ '*(*(of*(a)x)z)' ] }
Remove backslashes from returned matches.
Type: Boolean
Default: undefined
Example
In this example we want to match a literal *
:
mm.match(['abc', 'a\\*c'], 'a\\*c');
//=> ['a\\*c']
mm.match(['abc', 'a\\*c'], 'a\\*c', {unescape: true});
//=> ['a*c']
Convert path separators on returned files to posix/unix-style forward slashes.
Type: Boolean
Default: true
on windows, false
everywhere else
Example
mm.match(['a\\b\\c'], 'a/**');
//=> ['a/b/c']
mm.match(['a\\b\\c'], {unixify: false});
//=> ['a\\b\\c']
Micromatch also supports extended globbing features.
Extended globbing, as described by the bash man page:
pattern | regex equivalent | description |
---|---|---|
?(pattern-list) |
`(foo | bar)?` |
*(pattern-list) |
`(foo | bar)*` |
+(pattern-list) |
`(foo | bar)+` |
@(pattern-list) |
`(foo | bar)` * |
!(pattern-list) |
N/A (equivalent regex is much more complicated) | Matches anything except one of the given patterns |
* Note that @
isn't a RegEx character.
Powered by extglob. Visit that library for the full range of options or to report extglob related issues.
Expanded braces
Braces are expanded when ``
- range expansion:
a{1..3}b/*.js
expands to:['a1b/*.js', 'a2b/*.js', 'a3b/*.js']
- nesting:
a{c,{d,e}}b/*.js
expands to:['acb/*.js', 'adb/*.js', 'aeb/*.js']
Optimized braces (not expanded)
By default, brace patterns work the same way regex logical OR
operators. For example, (a|b)
will achieve the same result as {a,b}
.
Visit braces to see the full range of features and options related to brace expansion, or to create brace matching or expansion related issues.
Given the list: ['a.js', 'b.js', 'c.js', 'd.js', 'E.js']
:
[ac].js
: matches botha
andc
, returning['a.js', 'c.js']
[b-d].js
: matches fromb
tod
, returning['b.js', 'c.js', 'd.js']
[b-d].js
: matches fromb
tod
, returning['b.js', 'c.js', 'd.js']
a/[A-Z].js
: matches and uppercase letter, returning['a/E.md']
Learn about regex character classes.
Given ['a.js', 'b.js', 'c.js', 'd.js', 'E.js']
:
(a|c).js
: would match eithera
orc
, returning['a.js', 'c.js']
(b|d).js
: would match eitherb
ord
, returning['b.js', 'd.js']
(b|[A-Z]).js
: would match eitherb
or an uppercase letter, returning['b.js', 'E.js']
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.
POSIX brackets are intended to be more user-friendly than regex character classes. This of course is in the eye of the beholder.
Example
mm.isMatch('a1', '[[:alpha:][:digit:]]');
//=> true
mm.isMatch('a1', '[[:alpha:][:alpha:]]');
//=> false
See expand-brackets for more information about bracket expressions.
Whenever possible matching behavior is based on behavior Bash 4.3, which is mostly consistent with minimatch.
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.
There is an important, notable difference between minimatch and micromatch in regards to how backslashes are handled in glob patterns.
- Micromatch exclusively and explicitly reserves backslashes for escaping characters in a glob pattern, even on windows. This is consistent with bash behavior.
- Minimatch converts all backslashes to forward slashes, which means you can't use backslashes to escape any characters in your glob patterns.
We made this decision for micromatch for a couple of reasons:
- consistency with bash conventions.
- glob patterns are not filepaths. They are a type of 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.
A note about joining paths to globs
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 \\
.
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.
All contributions are welcome! Please read the contributing guide to get started.
Bug reports
Please feel free to create an issue if you find something that you think isn't right. However, if you find a matching-related issue, please try do the following first:
- research existing issues first
- visit the GNU Bash documentation to see how Bash deals with the pattern
- visit the [minimatch][] documentation to cross-check expected behavior
Install dev dependencies:
npm i -d && npm benchmark
As of {%= date() %} (longer bars are better):
{%= bench() %}