Glob matching for javascript/node.js, a replacement and faster alternative to minimatch and multimatch
90
Advanced pattern manipulation functions for creating regular expressions, parsing patterns, analyzing pattern structure, and extracting captured groups from matches.
Creates a regular expression from a glob pattern for advanced pattern matching or integration with other regex-based tools.
/**
* Create a regular expression from the given glob pattern
* @param {string} pattern - A glob pattern to convert to regex
* @param {object} options - See available options
* @returns {RegExp} Returns a regex created from the given pattern
*/
function makeRe(pattern, options);Usage Examples:
const { makeRe } = require('micromatch');
// Basic regex creation
const regex = makeRe('*.js');
console.log(regex);
//=> /^(?:(\.[\\\/])?(?!\.)(?=.)[^\/]*?\.js)$/
// Test with created regex
console.log(regex.test('app.js')); //=> true
console.log(regex.test('app.txt')); //=> false
// With options
const caseInsensitive = makeRe('*.JS', { nocase: true });
console.log(caseInsensitive.test('app.js')); //=> trueParses glob patterns to create source strings for regular expressions, with support for brace expansion preprocessing.
/**
* Parse a glob pattern to create the source string for a regular expression
* @param {string|string[]} patterns - Glob patterns to parse
* @param {object} options - See available options
* @returns {object[]} Returns array of parsed pattern objects with useful properties
*/
function parse(patterns, options);Usage Examples:
const { parse } = require('micromatch');
// Parse single pattern
const parsed = parse('src/**/*.js');
console.log(parsed[0].output); // Regex source string
console.log(parsed[0].state); // Parse state and metadata
// Parse with brace expansion
const bracePatterns = parse('src/{app,lib}/*.js');
// Returns separate parsed objects for 'src/app/*.js' and 'src/lib/*.js'Scans a glob pattern to separate it into segments for detailed analysis of pattern structure.
/**
* Scan a glob pattern to separate the pattern into segments
* @param {string} pattern - Pattern to scan
* @param {object} options - See available options
* @returns {object} Returns an object with pattern segments and metadata
*/
function scan(pattern, options);Usage Examples:
const { scan } = require('micromatch');
// Scan pattern structure
const scanned = scan('src/**/*.{js,ts}');
console.log(scanned.parts); // Pattern segments
console.log(scanned.globstar); // Boolean: contains **
console.log(scanned.negated); // Boolean: is negated patternExtracts captured groups from pattern matches, useful for extracting specific parts of matched strings.
/**
* Returns an array of matches captured by pattern in string, or null if no match
* @param {string} glob - Glob pattern to use for matching
* @param {string} input - String to match against
* @param {object} options - See available options for changing how matches are performed
* @returns {string[]|null} Returns array of captures if match found, otherwise null
*/
function capture(glob, input, options);Usage Examples:
const { capture } = require('micromatch');
// Basic capture
console.log(capture('test/*.js', 'test/foo.js'));
//=> ['foo']
// No match returns null
console.log(capture('test/*.js', 'src/bar.js'));
//=> null
// Multiple captures
console.log(capture('src/*/test/*.js', 'src/components/test/button.js'));
//=> ['components', 'button']
// Named directory capture
console.log(capture('**/docs/**/*.md', 'project/docs/api/readme.md'));
//=> ['project', 'api', 'readme']The parse() function automatically processes brace patterns before parsing:
// Input: 'src/{app,lib}/*.js'
// Expands to: ['src/app/*.js', 'src/lib/*.js']
// Then parses each expanded pattern separatelyParsed patterns include detailed state information:
output - The regex source stringstate.negated - Boolean indicating negationstate.globstar - Boolean indicating globstar usagestate.extglob - Boolean indicating extglob usagePattern processing functions handle path separator differences:
posixSlashes option for consistent forward slash outputThese functions largely delegate to the underlying picomatch library:
makeRe() calls picomatch.makeRe()scan() calls picomatch.scan()parse() adds brace expansion preprocessing before picomatch.parse()capture() uses picomatch.makeRe() with capture option enabledThis ensures consistent behavior with the core matching engine while providing convenient high-level interfaces.
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