Glob matching for javascript/node.js, a replacement and faster alternative to minimatch and multimatch
90
Functions that return boolean values for pattern matching tests, including single string tests, collection-based tests, and partial matching capabilities.
Tests if a single string matches any of the given glob patterns.
/**
* Returns true if any of the given glob patterns match the specified string
* @param {string} str - The string to test
* @param {string|string[]} patterns - One or more glob patterns to use for matching
* @param {object} options - See available options for changing how matches are performed
* @returns {boolean} Returns true if any patterns match str
*/
function isMatch(str, patterns, options);Usage Examples:
const { isMatch } = require('micromatch');
// Basic string matching
console.log(isMatch('foo.js', '*.js')); //=> true
console.log(isMatch('foo.txt', '*.js')); //=> false
// Multiple patterns
console.log(isMatch('a.a', ['b.*', '*.a'])); //=> true
// With options
console.log(isMatch('Foo.JS', '*.js', { nocase: true })); //=> trueBackwards compatibility alias for isMatch.
/**
* Alias for isMatch() - backwards compatibility
* @param {string} str - The string to test
* @param {string|string[]} patterns - One or more glob patterns
* @param {object} options - See available options
* @returns {boolean} Returns true if any patterns match str
*/
micromatch.any = isMatch;Creates a reusable matcher function from a glob pattern for efficient repeated testing.
/**
* Returns a matcher function from the given glob pattern and options
* @param {string} pattern - Glob pattern
* @param {object} options - See available options
* @returns {function} Returns a matcher function that takes a string and returns boolean
*/
function matcher(pattern, options);Usage Examples:
const { matcher } = require('micromatch');
// Create reusable matcher
const isJavaScript = matcher('*.js');
console.log(isJavaScript('app.js')); //=> true
console.log(isJavaScript('style.css')); //=> false
// With negation
const isNotTest = matcher('!*.test.js');
console.log(isNotTest('app.js')); //=> true
console.log(isNotTest('app.test.js')); //=> falseTests if a string contains a pattern anywhere within it (not just exact matching).
/**
* Returns true if the given string contains the given pattern
* @param {string} str - The string to match
* @param {string|string[]} patterns - Glob pattern to use for matching
* @param {object} options - See available options for changing how matches are performed
* @returns {boolean} Returns true if any of the patterns matches any part of str
*/
function contains(str, pattern, options);Usage Examples:
const { contains } = require('micromatch');
// Partial matching
console.log(contains('aa/bb/cc', '*b')); //=> true
console.log(contains('aa/bb/cc', '*d')); //=> false
// Path segments
console.log(contains('path/to/file.js', 'to')); //=> trueTests if some strings in a collection match any of the given patterns.
/**
* Returns true if some of the strings in the given list match any of the given glob patterns
* @param {string|string[]} list - The string or array of strings to test
* @param {string|string[]} patterns - One or more glob patterns to use for matching
* @param {object} options - See available options for changing how matches are performed
* @returns {boolean} Returns true if any patterns matches any of the strings in list
*/
function some(list, patterns, options);Usage Examples:
const { some } = require('micromatch');
console.log(some(['foo.js', 'bar.js'], ['*.js', '!foo.js'])); //=> true
console.log(some(['foo.js'], ['*.js', '!foo.js'])); //=> falseTests if every string in a collection matches any of the given patterns.
/**
* Returns true if every string in the given list matches any of the given glob patterns
* @param {string|string[]} list - The string or array of strings to test
* @param {string|string[]} patterns - One or more glob patterns to use for matching
* @param {object} options - See available options for changing how matches are performed
* @returns {boolean} Returns true if all patterns matches all of the strings in list
*/
function every(list, patterns, options);Usage Examples:
const { every } = require('micromatch');
console.log(every('foo.js', ['foo.js'])); //=> true
console.log(every(['foo.js', 'bar.js'], ['*.js'])); //=> true
console.log(every(['foo.js', 'bar.js'], ['*.js', '!foo.js'])); //=> falseTests if ALL given patterns match a single string (stricter than isMatch which tests if ANY pattern matches).
/**
* Returns true if all of the given patterns match the specified string
* @param {string} str - The string to test
* @param {string|string[]} patterns - One or more glob patterns to use for matching
* @param {object} options - See available options for changing how matches are performed
* @returns {boolean} Returns true if all patterns match str
*/
function all(str, patterns, options);Usage Examples:
const { all } = require('micromatch');
console.log(all('foo.js', ['foo.js'])); //=> true
console.log(all('foo.js', ['*.js', '!foo.js'])); //=> false
console.log(all('foo.js', ['*.js', 'foo.js'])); //=> true
console.log(all('foo.js', ['*.js', 'f*', '*o*', '*o.js'])); //=> trueTypeError for invalid string input in contains() functionfalse for non-matching patterns (does not throw)Install with Tessl CLI
npx tessl i tessl/npm-micromatchdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10