Glob matching for javascript/node.js. A 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 :heart: and support.
Details
Install with npm:
$ npm install --save micromatch
Become a Sponsor to add your logo to this README, or any of my other projects
const micromatch = require('micromatch');
// micromatch(list, patterns[, options]);
The main export takes a list of strings and one or more glob patterns:
console.log(micromatch(['foo', 'bar', 'baz', 'qux'], ['f*', 'b*'])) //=> ['foo', 'bar', 'baz']
console.log(micromatch(['foo', 'bar', 'baz', 'qux'], ['*', '!b*'])) //=> ['foo', 'qux']
Use .isMatch() to for boolean matching:
console.log(micromatch.isMatch('foo', 'f*')) //=> true
console.log(micromatch.isMatch('foo', ['b*', 'f*'])) //=> true
Switching from minimatch and multimatch is easy!
micromatch is a replacement for minimatch and multimatch
* and ?), globstars (**) for nested directories\ or quotes.**, *.js)'!a/*.js', '*!(b).js')+(x|y), !(a|b))[[:alpha:][:digit:]])foo/{1..5}.md, bar/{a,b,c}.js)foo-[1-5].js)foo/(abc|xyz).js)You can mix and match these features to create whatever patterns you need!
(There is one notable difference between micromatch and minimatch in regards to how backslashes are handled. See the notes about backslashes for more information.)
Use micromatch.isMatch() instead of minimatch():
console.log(micromatch.isMatch('foo', 'b*')); //=> false
Use micromatch.match() instead of minimatch.match():
console.log(micromatch.match(['foo', 'bar'], 'b*')); //=> 'bar'
Same signature:
console.log(micromatch(['foo', 'bar', 'baz'], ['f*', '*z'])); //=> ['foo', 'baz']
Params
list {String|Array}: List of strings to match.patterns {String|Array}: One or more glob patterns to use for matching.options {Object}: See available optionsreturns {Array}: Returns an array of matchesExample
const mm = require('micromatch');
// mm(list, patterns[, options]);
console.log(mm(['a.js', 'a.txt'], ['*.js']));
//=> [ 'a.js' ]
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 patternoptions {Object}returns {Function}: Returns a matcher function.Example
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
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.returns {Boolean}: Returns true if any patterns match strExample
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
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
const mm = require('micromatch');
// mm.not(list, patterns[, options]);
console.log(mm.not(['a.a', 'b.b', 'c.c'], '*.a'));
//=> ['b.b', 'c.c']
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 any of the patterns matches any part of str.Example
var mm = require('micromatch');
// mm.contains(string, pattern[, options]);
console.log(mm.contains('aa/bb/cc', '*b'));
//=> true
console.log(mm.contains('aa/bb/cc', '*d'));
//=> false
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
const mm = require('micromatch');
// mm.matchKeys(object, patterns[, options]);
const obj = { aa: 'a', ab: 'b', ac: 'c' };
console.log(mm.matchKeys(obj, '*b'));
//=> { ab: 'b' }
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 matches any of the strings in listExample
const mm = require('micromatch');
// mm.some(list, patterns[, options]);
console.log(mm.some(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
// true
console.log(mm.some(['foo.js'], ['*.js', '!foo.js']));
// false
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 all patterns matches all of the strings in listExample
const mm = require('micromatch');
// mm.every(list, patterns[, options]);
console.log(mm.every('foo.js', ['foo.js']));
// true
console.log(mm.every(['foo.js', 'bar.js'], ['*.js']));
// true
console.log(mm.every(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
// false
console.log(mm.every(['foo.js'], ['*.js', '!foo.js']));
// false
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 match strExample
const mm = require('micromatch');
// mm.all(string, patterns[, options]);
console.log(mm.all('foo.js', ['foo.js']));
// true
console.log(mm.all('foo.js', ['*.js', '!foo.js']));
// false
console.log(mm.all('foo.js', ['*.js', 'foo.js']));
// true
console.log(mm.all('foo.js', ['*.js', 'f*', '*o*', '*o.js']));
// true
Returns an array of matches captured by pattern in string, ornull` if the pattern did not match.
Params
glob {String}: Glob pattern to use for matching.input {String}: String to matchoptions {Object}: See available options for changing how matches are performedreturns {Array|null}: Returns an array of captures if the input matches the glob pattern, otherwise null.Example
const mm = require('micromatch');
// mm.capture(pattern, string[, options]);
console.log(mm.capture('test/*.js', 'test/foo.js'));
//=> ['foo']
console.log(mm.capture('test/*.js', 'foo/bar.css'));
//=> null
Create a regular expression from the given glob pattern.
Params
pattern {String}: A glob pattern to convert to regex.options {Object}returns {RegExp}: Returns a regex created from the given pattern.Example
const mm = require('micromatch');
// mm.makeRe(pattern[, options]);
console.log(mm.makeRe('*.js'));
//=> /^(?:(\.[\\\/])?(?!\.)(?=.)[^\/]*?\.js)$/
Scan a glob pattern to separate the pattern into segments. Used by the split method.
Params
pattern {String}options {Object}returns {Object}: Returns an object withExample
const mm = require('micromatch');
const state = mm.scan(pattern[, options]);
Parse a glob pattern to create the source string for a regular expression.
Params
glob {String}options {Object}returns {Object}: Returns an object with useful properties and output to be used as regex source string.Example
const mm = require('micromatch');
const state = mm.parse(pattern[, options]);
Process the given brace pattern.
Params
pattern {String}: String with brace pattern to process.options **{Obje$ claude mcp add micromatch \
-- python -m otcore.mcp_server <graph>