micromatch
Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.
Table of Contents
Install
Install with npm:
Quickstart
The main export takes a list of strings and one or more glob patterns:
Use .isMatch() to get true/false:
Switching from minimatch and multimatch is easy!
Why use micromatch?
micromatch is a drop-in replacement for minimatch and multimatch
Supports all of the same matching features as minimatch and multimatch
Micromatch uses 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.
More consistently accurate matching than minimatch, with more than 36,000 test assertions to prove it.
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.
Faster matching, from a combination of optimized glob patterns, faster algorithms, and regex caching.
Micromatch is safer, and is not subject to DoS with brace patterns, like minimatch and multimatch.
More reliable windows support than minimatch and multimatch.
Matching features
Support for multiple glob patterns (no need for wrappers like multimatch)
Wildcards (
**
,*.js
)Negation (
'!a/*.js'
,'*!(b).js']
)extglobs (
+(x|y)
,!(a|b)
)POSIX character classes (
[[:alpha:][:digit:]]
)brace expansion (
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 for more information.
From minimatch
Use mm.isMatch() instead of minimatch()
:
Use mm.match() instead of minimatch.match()
:
From multimatch
Same signature:
API
The main function takes a list of strings and one or more glob patterns to use for matching.
Params
list
{Array}: A list of strings to matchpatterns
{String|Array}: One or more glob patterns to use for matching.options
{Object}: See available options for changing how matches are performedreturns
{Array}: Returns an array of matches
Example
Similar to the main function, but pattern
must be a string.
Params
list
{Array}: Array of strings to matchpattern
{String}: Glob pattern to use for matching.options
{Object}: See available options for changing how matches are performedreturns
{Array}: Returns an array of matches
Example
Returns true if the specified string
matches the given glob pattern
.
Params
string
{String}: String to matchpattern
{String}: Glob pattern to use for matching.options
{Object}: See available options for changing how matches are performedreturns
{Boolean}: Returns true if the string matches the glob pattern.
Example
Returns true if some of the strings in the given list
match any of the given glob patterns
.
Params
list
{String|Array}: The string or array of strings to test. Returns as soon as the first match is found.patterns
{String|Array}: One or more glob patterns to use for matching.options
{Object}: See available options for changing how matches are performedreturns
{Boolean}: Returns true if any patterns matchstr
Example
Returns true if every string in the given list
matches any of the given glob patterns
.
Params
list
{String|Array}: The string or array of strings to test.patterns
{String|Array}: One or more glob patterns to use for matching.options
{Object}: See available options for changing how matches are performedreturns
{Boolean}: Returns true if any patterns matchstr
Example
Returns true if any of the given glob patterns
match the specified string
.
Params
str
{String|Array}: The string to test.patterns
{String|Array}: One or more glob patterns to use for matching.options
{Object}: See available options for changing how matches are performedreturns
{Boolean}: Returns true if any patterns matchstr
Example
Returns true if all of the given patterns
match the specified string.
Params
str
{String|Array}: The string to test.patterns
{String|Array}: One or more glob patterns to use for matching.options
{Object}: See available options for changing how matches are performedreturns
{Boolean}: Returns true if any patterns matchstr
Example
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 for changing how matches are performedreturns
{Array}: Returns an array of strings that do not match the given patterns.
Example
Returns true if the given string
contains the given pattern. Similar to .isMatch but the pattern can match any part of the string.
Params
str
{String}: The string to match.patterns
{String|Array}: Glob pattern to use for matching.options
{Object}: See available options for changing how matches are performedreturns
{Boolean}: Returns true if the patter matches any part ofstr
.
Example
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 instead.
Params
object
{Object}: The object with keys to filter.patterns
{String|Array}: One or more glob patterns to use for matching.options
{Object}: See available options for changing how matches are performedreturns
{Object}: Returns an object with only keys that match the given patterns.
Example
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.
Params
pattern
{String}: Glob patternoptions
{Object}: See available options for changing how matches are performed.returns
{Function}: Returns a matcher function.
Example
Returns an array of matches captured by pattern
in string, or
null` if the pattern did not match.
Params
pattern
{String}: Glob pattern to use for matching.string
{String}: String to matchoptions
{Object}: See available options for changing how matches are performedreturns
{Boolean}: Returns an array of captures if the string matches the glob pattern, otherwisenull
.
Example
Create a regular expression from the given glob pattern
.
Params
pattern
{String}: A glob pattern to convert to regex.options
{Object}: See available options for changing how matches are performed.returns
{RegExp}: Returns a regex created from the given pattern.
Example
Expand the given brace pattern
.
Params
pattern
{String}: String with brace pattern to expand.returns
{Array}
Example
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.
Params
pattern
{String}: Glob pattern to parse and compile.options
{Object}: Any options to change how parsing and compiling is performed.returns
{Object}: Returns an object with the parsed AST, compiled string and optional source map.
Example
Parse the given str
with the given options
.
Params
str
{String}options
{Object}returns
{Object}: Returns an AST
Example
Compile the given ast
or string with the given options
.
Params
ast
{Object|String}options
{Object}returns
{Object}: Returns an object that has anoutput
property with the compiled string.
Example
Clear the regex cache.
Example
Options
options.basename
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
options.bash
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
options.cache
Disable regex and function memoization.
Type: Boolean
Default: undefined
options.dot
Match dotfiles. Same behavior as minimatch option dot
.
Type: Boolean
Default: false
options.failglob
Similar to the --failglob
behavior in Bash, throws an error when no matches are found.
Type: Boolean
Default: undefined
options.ignore
String or array of glob patterns to match files to ignore.
Type: String|Array
Default: undefined
options.matchBase
Alias for options.basename.
options.nobrace
Disable expansion of brace patterns. Same behavior as minimatch option nobrace
.
Type: Boolean
Default: undefined
See braces for more information about extended brace expansion.
options.nocase
Use a case-insensitive regex for matching files. Same behavior as minimatch.
Type: Boolean
Default: undefined
options.nodupes
Remove duplicate elements from the result array.
Type: Boolean
Default: undefined
Example
Example of using the unescape
and nodupes
options together:
options.noext
Disable extglob support, so that extglobs are regarded as literal characters.
Type: Boolean
Default: undefined
Examples
options.nonegate
Disallow negation (!
) patterns, and treat leading !
as a literal character to match.
Type: Boolean
Default: undefined
options.noglobstar
Disable matching with globstars (**
).
Type: Boolean
Default: undefined
options.nonull
Alias for options.nullglob.
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
options.snapdragon
Pass your own instance of snapdragon, to customize parsers or compilers.
Type: Object
Default: undefined
options.sourcemap
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
options.unescape
Remove backslashes from returned matches.
Type: Boolean
Default: undefined
Example
In this example we want to match a literal *
:
options.unixify
Convert path separators on returned files to posix/unix-style forward slashes.
Type: Boolean
Default: true
on windows, false
everywhere else
Example
Extended globbing
Micromatch also supports extended globbing features.
extglobs
Extended globbing, as described by the bash man page:
pattern | regex equivalent | description |
|
| Matches zero or one occurrence of the given patterns |
|
| Matches zero or more occurrences of the given patterns |
|
| Matches one or more occurrences of the given patterns |
|
| Matches one of the given patterns |
| 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.
braces
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:
Visit braces to see the full range of features and options related to brace expansion, or to create brace matching or expansion related issues.
regex character classes
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.
regex groups
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 bracket expressions
POSIX brackets are intended to be more user-friendly than regex character classes. This of course is in the eye of the beholder.
Example
See expand-brackets for more information about bracket expressions.
Notes
Bash 4.3 parity
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.
Backslashes
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.
Contributing
All contributions are welcome! Please read the contributing guide to get started.
Bug reports
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:
research existing issues first (open and closed)
visit the GNU Bash documentation to see how Bash deals with the pattern
visit the minimatch documentation to cross-check expected behavior in node.js
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.
Platform issues
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).
Benchmarks
Running benchmarks
Install dev dependencies:
Latest results
As of February 18, 2018 (longer bars are better):
About
Related projects
You might also be interested in these projects:
expand-brackets: Expand POSIX bracket expressions (character classes) in glob patterns. | homepage
fill-range: Fill in a range of numbers or letters, optionally passing an increment or
step
to… more | homepage
Contributors
Commits | Contributor |
457 | |
12 | |
8 | |
3 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |
Author
Jon Schlinkert
License
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on February 18, 2018.
Last updated