CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-micromatch

Glob matching for javascript/node.js, a replacement and faster alternative to minimatch and multimatch

90

1.50x
Overview
Eval results
Files

pattern-processing.mddocs/

Pattern Processing

Advanced pattern manipulation functions for creating regular expressions, parsing patterns, analyzing pattern structure, and extracting captured groups from matches.

Capabilities

Regular Expression Creation

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')); //=> true

Pattern Parsing

Parses 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'

Pattern Scanning

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 pattern

Capture Groups

Extracts 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']

Advanced Pattern Features

Brace Preprocessing

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 separately

Pattern State Information

Parsed patterns include detailed state information:

  • output - The regex source string
  • state.negated - Boolean indicating negation
  • state.globstar - Boolean indicating globstar usage
  • state.extglob - Boolean indicating extglob usage
  • And many other metadata properties

Cross-Platform Handling

Pattern processing functions handle path separator differences:

  • Automatically converts Windows backslashes when needed
  • Respects posixSlashes option for consistent forward slash output
  • Handles platform-specific path patterns correctly

Integration with Picomatch

These 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 enabled

This ensures consistent behavior with the core matching engine while providing convenient high-level interfaces.

Install with Tessl CLI

npx tessl i tessl/npm-micromatch

docs

boolean-testing.md

brace-expansion.md

index.md

main-matching.md

pattern-processing.md

utility-functions.md

tile.json