Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.
Micromatch supports all of the same matching features as minimatch and multimatch.
- mm() is the same as multimatch()
- mm.match() is the same as minimatch.match()
- use mm.isMatch() instead of minimatch()
Install with npm:
$ npm install micromatch --save
var mm = require('micromatch');
console.log(mm(['']))
- Drop-in replacement for minimatch and multimatch
- Built-in support for multiple glob patterns, like
['foo/*.js', '!bar.js']
- Brace Expansion (
foo/bar-{1..5}.md
,one/{two,three}/four.md
) - Typical glob patterns, like
**/*
,a/b/*.js
, or['foo/*.js', '!bar.js']
- Methods like
.isMatch()
,.contains()
and.any()
Extended globbing features:
- Logical
OR
(foo/bar/(abc|xyz).js
) - Regex character classes (
foo/bar/baz-[1-5].js
) - POSIX bracket expressions (
**/[[:alpha:][:digit:]]/
) - extglobs (
**/+(x|y)
,!(a|b)
, etc).
You can combine these to create whatever matching patterns you need.
Example
// double-negation!
mm(['fa', 'fb', 'f', 'fo'], '!(f!(o))');
//=> ['fo']
- Native support for multiple glob patterns, no need for wrappers like multimatch
- 10-55x faster and more performant than minimatch and multimatch. This is achieved through a combination of caching and regex optimization strategies, a fundamentally different approach than minimatch.
- More extensive support for the Bash 4.3 specification
- More complete extglob support
- Extensive unit tests (approx. 1,300 tests). Minimatch fails many of the tests.
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']
Add micromatch to your node.js project:
var mm = require('micromatch');
Signature
mm(array_of_strings, glob_patterns[, options]);
Example
mm(['foo', 'bar', 'baz'], 'b*');
//=> ['bar', 'baz']
Brace expansion
Match files with .js
or .txt
extensions.
mm(['a.js', 'b.md', 'c.txt'], '*.{js,txt}');
//=> ['a.js', 'c.txt']
Extglobs
Match anything except for files with the .md
extension.
mm(files, '**/*.!(md)');
//=> ['a.js', 'c.txt']
Multiple patterns
Match using an array of patterns.
mm(['a.md', 'b.js', 'c.txt', 'd.json'], ['*.md', '*.txt']);
//=> ['a.md', 'c.txt']
Negation patterns:
Behavior is designed to be what users would expect, based on conventions that are already well-established.
- minimatch behavior is used when the pattern is a string, so patterns are inclusive by default.
- multimatch behavior is used when an array of patterns is passed, so patterns are exclusive by default.
mm(['a.js', 'b.md', 'c.txt'], '!*.{js,txt}');
//=> ['b.md']
mm(['a.md', 'b.js', 'c.txt', 'd.json'], ['*.*', '!*.{js,txt}']);
//=> ['a.md', 'd.json']
var mm = require('micromatch');
mm.match(array, globString);
Return an array of files that match the given glob pattern. Useful if you only need to use a single glob pattern.
Example
mm.match(['ab', 'a/b', 'bb', 'b/c'], '?b');
//=> ['ab', 'bb']
mm.match(['ab', 'a/b', 'bb', 'b/c'], '*/b');
//=> ['a/b']
mm.isMatch(filepath, globString);
Returns true if a file path matches the given glob pattern.
Example
mm.isMatch('.verb.md', '*.md');
//=> false
mm.isMatch('.verb.md', '*.md', {dot: true});
//=> true
Returns true if any part of a file path matches the given glob pattern. Think of this is "has path" versus "is path".
Example
.isMatch()
would return false for both of the following:
mm.contains('a/b/c', 'a/b');
//=> true
mm.contains('a/b/c', 'a/*');
//=> true
Returns a function for matching using the supplied pattern. e.g. create your own "matcher". The advantage of this method is that the pattern can be compiled outside of a loop.
Pattern
Can be any of the following:
glob/string
regex
function
Example
var isMatch = mm.matcher('*.md');
var files = [];
['a.md', 'b.txt', 'c.md'].forEach(function(fp) {
if (isMatch(fp)) {
files.push(fp);
}
});
Returns a function that can be passed to Array#filter()
.
Params
patterns
{String|Array}:
Examples
Single glob:
var fn = mm.filter('*.md');
['a.js', 'b.txt', 'c.md'].filter(fn);
//=> ['c.md']
var fn = mm.filter('[a-c]');
['a', 'b', 'c', 'd', 'e'].filter(fn);
//=> ['a', 'b', 'c']
Array of glob patterns:
var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
var fn = mm.filter(['{1..10}', '![7-9]', '!{3..4}']);
arr.filter(fn);
//=> [1, 2, 5, 6, 10]
(Internally this function generates the matching function by using the matcher method. You can use the matcher method directly to create your own filter function)
Returns true if a file path matches any of the given patterns.
mm.any(filepath, patterns, options);
Params
- filepath
{String}
: The file path to test. - patterns
{String|Array}
: One or more glob patterns - options:
{Object}
: options to pass to the.matcher()
method.
Example
mm.any('abc', ['!*z']);
//=> true
mm.any('abc', ['a*', 'z*']);
//=> true
mm.any('abc', 'a*');
//=> true
mm.any('abc', ['z*']);
//=> false
Returns an object with a regex-compatible string and tokens.
mm.expand('*.js');
// when `track` is enabled (for debugging), the `history` array is used
// to record each mutation to the glob pattern as it's converted to regex
{ options: { track: false, dot: undefined, makeRe: true, negated: false },
pattern: '(.*\\/|^)bar\\/(?:(?!(?:^|\\/)\\.).)*?',
history: [],
tokens:
{ path:
{ whole: '**/bar/**',
dirname: '**/bar/',
filename: '**',
basename: '**',
extname: '',
ext: '' },
is:
{ glob: true,
negated: false,
globstar: true,
dotfile: false,
dotdir: false },
match: {},
original: '**/bar/**',
pattern: '**/bar/**',
base: '' } }
Create a regular expression for matching file paths based on the given pattern:
mm.makeRe('*.js');
//=> /^(?:(?!\.)(?=.)[^/]*?\.js)$/
Normalize slashes in file paths and glob patterns to forward slashes.
Type: {Boolean}
Default: undefined
on non-windows, true
on windows.
Match dotfiles. Same behavior as minimatch.
Type: {Boolean}
Default: false
Unescape slashes in glob patterns. Use cautiously, especially on windows.
Type: {Boolean}
Default: undefined
Example
mm.isMatch('abc', '\\a\\b\\c', {unescape: true});
//=> true
Remove duplicate elements from the result array.
Type: {Boolean}
Default: undefined
Example
Example of using the unescape
and nodupes
options together:
mm.match(['abc', '\\a\\b\\c'], '\\a\\b\\c', {unescape: true});
//=> ['abc', 'abc']
mm.match(['abc', '\\a\\b\\c'], '\\a\\b\\c', {unescape: true, nodupes: true});
//=> ['abc']
Allow glob patterns without slashes to match a file path based on its basename. . Same behavior as minimatch.
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']
Don't expand braces in glob patterns. Same behavior as minimatch nobrace
.
Type: {Boolean}
Default: undefined
See braces for more information about extended brace expansion.
Don't expand POSIX bracket expressions.
Type: {Boolean}
Default: undefined
See expand-brackets for more information about extended bracket expressions.
Don't expand extended globs.
Type: {Boolean}
Default: undefined
See extglob for more information about extended globs.
Use a case-insensitive regex for matching files. Same behavior as minimatch.
Type: {Boolean}
Default: false
Disallow negation (!
) patterns.
Type: {Boolean}
Default: false
If true
, when no matches are found the actual (array-ified) glob pattern is returned instead of an empty array. Same behavior as minimatch.
Type: {Boolean}
Default: false
Cache the platform (e.g. win32
) to prevent this from being looked up for every filepath.
Type: {Boolean}
Default: true
Micromatch also supports the following.
Extended globbing, as described by the bash man page:
pattern | regex equivalent | description |
---|---|---|
?(pattern-list) |
`(... | ...)?` |
*(pattern-list) |
`(... | ...)*` |
+(pattern-list) |
`(... | ...)+` |
@(pattern-list) |
`(... | ...)` * |
!(pattern-list) |
N/A | Matches anything except one of the given patterns |
* @
isn't a RegEx character.
Powered by extglob. Visit that library for the full range of options or to report extglob related issues.
See extglob for more information about extended globs.
In simple cases, brace expansion appears to work the same way as the logical OR
operator. For example, (a|b)
will achieve the same result as {a,b}
.
Here are some powerful features unique to brace expansion (versus character classes):
- 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']
Visit braces to ask questions and create an issue related to brace-expansion, or to see the full range of features and options related to brace expansion.
With the exception of brace expansion ({a,b}
, {1..5}
, etc), most of the special characters convert directly to regex, so you can expect them to follow the same rules and produce the same results as regex.
For example, 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, parenthese can be nested, so patterns like ((a|b)|c)/b
will work. But it might be easier to achieve your goal using brace expansion.
Example
mm.isMatch('a1', '[[:alpha:][:digit:]]');
//=> true
See expand-brackets for more information about extended bracket expressions.
Whenever possible parsing behavior for patterns is based on globbing specifications in Bash 4.3. Patterns that aren't described by Bash follow wildmatch spec (used by git).
Run the benchmarks:
node benchmark
As of April 22, 2016:
#1: basename-braces
micromatch x 26,420 ops/sec ±0.89% (91 runs sampled)
minimatch x 3,507 ops/sec ±0.64% (97 runs sampled)
#2: basename
micromatch x 25,315 ops/sec ±0.82% (93 runs sampled)
minimatch x 4,398 ops/sec ±0.86% (94 runs sampled)
#3: braces-no-glob
micromatch x 341,254 ops/sec ±0.78% (93 runs sampled)
minimatch x 30,197 ops/sec ±1.12% (91 runs sampled)
#4: braces
micromatch x 54,649 ops/sec ±0.74% (94 runs sampled)
minimatch x 3,095 ops/sec ±0.82% (95 runs sampled)
#5: immediate
micromatch x 16,719 ops/sec ±0.79% (95 runs sampled)
minimatch x 4,348 ops/sec ±0.86% (96 runs sampled)
#6: large
micromatch x 721 ops/sec ±0.77% (94 runs sampled)
minimatch x 17.73 ops/sec ±1.08% (50 runs sampled)
#7: long
micromatch x 5,051 ops/sec ±0.87% (97 runs sampled)
minimatch x 628 ops/sec ±0.83% (94 runs sampled)
#8: mid
micromatch x 51,280 ops/sec ±0.80% (95 runs sampled)
minimatch x 1,923 ops/sec ±0.84% (95 runs sampled)
#9: multi-patterns
micromatch x 22,440 ops/sec ±0.97% (94 runs sampled)
minimatch x 2,481 ops/sec ±1.10% (94 runs sampled)
#10: no-glob
micromatch x 722,823 ops/sec ±1.30% (87 runs sampled)
minimatch x 52,967 ops/sec ±1.09% (94 runs sampled)
#11: range
micromatch x 243,471 ops/sec ±0.79% (94 runs sampled)
minimatch x 11,736 ops/sec ±0.82% (96 runs sampled)
#12: shallow
micromatch x 190,874 ops/sec ±0.98% (95 runs sampled)
minimatch x 21,699 ops/sec ±0.81% (97 runs sampled)
#13: short
micromatch x 496,393 ops/sec ±3.86% (90 runs sampled)
minimatch x 53,765 ops/sec ±0.75% (95 runs sampled)
Install dev dependencies:
$ npm install -d && npm test
As of April 22, 2016:
Statements : 100% (441/441)
Branches : 100% (270/270)
Functions : 100% (54/54)
Lines : 100% (429/429)
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Please be sure to run the benchmarks before/after any code changes to judge the impact before you do a PR. thanks!
- braces: Fastest brace expansion for node.js, with the most complete support for the Bash 4.3 braces… more | homepage
- expand-brackets: Expand POSIX bracket expressions (character classes) in glob patterns. | homepage
- expand-range: Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. See… more | homepage
- extglob: Convert extended globs to regex-compatible strings. Add (almost) the expressive power of regular expressions to… more | homepage
- fill-range: Fill in a range of numbers or letters, optionally passing an increment or multiplier to… more | homepage
- gulp-micromatch: Filter vinyl files with glob patterns, string, regexp, array, object or matcher function. micromatch stream. | homepage
- is-glob: Returns
true
if the given string looks like a glob pattern or an extglob pattern.… more | homepage - parse-glob: Parse a glob pattern into an object of tokens. | homepage
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Generate readme and API documentation with verb:
$ npm install verb && npm run docs
Or, if verb is installed globally:
$ verb
Install dev dependencies:
$ npm install -d && npm test
Jon Schlinkert
Copyright © 2016, Jon Schlinkert. Released under the MIT license.
This file was generated by verb, v0.9.0, on April 22, 2016.