Glob Pattern Matcher

10.2.5 · active · verified Sun Apr 19

Minimatch is a JavaScript utility library that provides robust glob matching functionality, converting glob expressions into JavaScript `RegExp` objects for efficient pattern matching. It is famously used internally by npm for its file system operations. The current stable version is 10.2.5, with releases typically occurring as needed to address bugs, enhance features, or align with npm's requirements. Key features include support for brace expansion, extended glob matching, globstar (`**`), and Posix character classes (e.g., `[[:alpha:]]`), which are Unicode-aware. A critical aspect of minimatch is its explicit warning regarding Regular Expression Denial of Service (ReDoS) vulnerabilities, advising users to never use untrusted input as glob patterns due to the inherent risks of RegExp-based matching. It also provides specific guidance for Windows users, emphasizing the exclusive use of forward slashes in glob expressions to avoid misinterpretation of backslashes as escape characters.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic glob matching, usage with options, and pre-compiling patterns with the `Minimatch` class, including Unicode-aware Posix character classes.

import { minimatch, Minimatch } from 'minimatch';

// Basic usage
console.log(minimatch('foo/bar/baz.js', 'foo/**/baz.js')); // true
console.log(minimatch('foo/bar/file.txt', '*.txt'));       // false (needs path)
console.log(minimatch('file.txt', '*.txt'));               // true

// With options: debug and nobrace
const pattern = 'a/{b,c}/d';
const options = { debug: false, nobrace: false };
console.log(minimatch('a/b/d', pattern, options)); // true
console.log(minimatch('a/c/d', pattern, options)); // true

// Using the Minimatch class for pre-compiled patterns
const mm = new Minimatch('src/**/*.ts', { matchBase: true });
console.log(mm.match('src/components/button.ts')); // true
console.log(mm.match('dist/index.js'));             // false

// Example with Posix character classes (Unicode aware)
console.log(minimatch('é', '[[:alpha:]]')); // true
console.log(minimatch('123', '[[:digit:]]')); // false

view raw JSON →