Micromatch Glob Matching Utility

4.0.8 · active · verified Sun Apr 19

Micromatch is a comprehensive and highly performant JavaScript utility for glob matching, serving as a faster and more feature-rich alternative to older libraries like minimatch and multimatch. The current stable version is 4.0.8, actively maintained with regular updates to address bug fixes and security vulnerabilities. Key differentiators include its speed and extensive support for the Bash 4.3 specification, often surpassing Bash itself in terms of adherence to its glob test suite. It is widely adopted across the JavaScript ecosystem for file system operations, build tooling, and configuration management, providing a robust solution for pattern matching in both Node.js and browser environments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic glob matching, filtering lists of strings, using negation patterns, and performing boolean checks with micromatch. It covers common scenarios like selecting files by extension or excluding specific types, showcasing the flexibility of its API.

import micromatch from 'micromatch';

// Basic usage: filter a list of strings against glob patterns
const files = ['foo.txt', 'bar.js', 'baz.md', 'qux.ts', 'image.png'];
const jsAndMdFiles = micromatch(files, ['*.js', '*.md']);
console.log('Matching .js and .md files:', jsAndMdFiles); // => ['bar.js', 'baz.md']

// Using negation to exclude patterns
const allButJsFiles = micromatch(files, ['*', '!*.js']);
console.log('All files except .js:', allButJsFiles); // => ['foo.txt', 'baz.md', 'qux.ts', 'image.png']

// Boolean matching for a single string
const isSourceFile = micromatch.isMatch('src/index.ts', ['src/**/*.ts', 'src/**/*.tsx']);
console.log('Is src/index.ts a source file?', isSourceFile); // => true

const isConfigFile = micromatch.isMatch('config.json', '**/config.json');
console.log('Is config.json a config file?', isConfigFile); // => true

view raw JSON →