The glob matching library used internally by npm, converting glob expressions to JavaScript RegExp. Current version 10.x, ESM/CJS dual mode.
Use when the user needs glob matching, file pattern matching, or .gitignore-style matching.
npm install minimatch
import { minimatch } from 'minimatch';
// or: const { minimatch } = require('minimatch');
minimatch('bar.foo', '*.foo'); // true
minimatch('bar.foo', '*.bar'); // false
minimatch('bar.foo', '*.+(bar|foo)', { debug: true }); // true + stderr debug output
{a,b}, {1..3}+(a|b), *(a|b), ?(a|b), @(a|b), !(a|b)** matches any number of directory levels[[:alpha:]] (full Unicode support, e.g., é)minimatch(path, pattern, options?)Test whether a path matches a pattern:
minimatch('src/app.js', '*.js', { matchBase: true }); // true
minimatch.filter(pattern, options?)Returns a filter function that can be passed to Array.filter:
const jsFiles = fileList.filter(minimatch.filter('*.js', { matchBase: true }));
minimatch.match(list, pattern, options?)Performs fnmatch/glob style matching on a list of files. Returns the pattern itself when no matches are found and nonull: true:
minimatch.match(fileList, '*.js', { matchBase: true });
minimatch.escape(pattern)Escapes all special characters in a glob pattern so it will match literal text only:
minimatch.escape('*.js'); // '\*.js'
minimatch.unescape(pattern)Unescapes a pattern:
minimatch.unescape('\\*.js'); // '*.js'
minimatch.makeRe(pattern, options?)Generates a RegExp object from a pattern:
const re = minimatch.makeRe('*.js');
re.test('foo.js'); // true
import { Minimatch } from 'minimatch';
const mm = new Minimatch('**/*.js', { dot: true });
mm.match('src/foo.js'); // true
mm.match('.hidden.js'); // true (because dot: true)
mm.hasMagic(); // true
mm.makeRe(); // Returns RegExp
mm.negate; // false — whether it's a negated ! pattern
mm.comment; // false — whether it's a # comment pattern
mm.matchOne(fileArray, patternArray, partial?)Matches path components after splitting by /, primarily used by glob-walkers to reduce filesystem calls.
false)| Option | Description | |
|---|---|---|
| -------- | ------------- | |
debug | Output debug information to stderr | |
nobrace | Disable {a,b} and {1..3} brace expansion | |
noglobstar | Disable ** multi-level directory matching | |
dot | Allow matching filenames starting with . (disabled by default) | |
noext | Disable extglob patterns such as `+(a\ | b)` |
nocase | Case-insensitive matching | |
nocaseMagicOnly | Only effective when nocase: true, makes case-insensitive only for parts containing magic characters | |
nonull | When minimatch.match finds no matches, return [pattern] instead of [] | |
magicalBraces | Affects hasMagic(): treats braces without other magic characters as magic | |
matchBase | Patterns without / match against the path basename | |
nocomment | Disable comment patterns starting with # | |
nonegate | Disable negated ! patterns | |
flipNegate | Reverse the result of negated patterns (return false on match) | |
partial | Partial path matching, used when traversing directory trees to determine if a match is possible | |
windowsPathsNoEscape | On Windows, \ acts only as a path separator, not an escape character | |
windowsNoMagicRoot | On Windows + nocase, do NOT make UNC roots/drive letters case-insensitive | |
preserveMultipleSlashes | Preserve consecutive / (default a///b matches a/b) | |
optimizationLevel | Optimization level 0/1/2 (default 1), see reference documentation | |
platform | Defaults to process.platform, setting to 'win32' triggers Windows behavior |
minimatch uses JavaScript regular expressions. Never pass user input as a pattern to this library —
> If you build a system that takes user input and uses it directly as a regex pattern, whether with minimatch or any JS glob matcher, you will be pwned.
Future versions may switch to a non-backtracking matching algorithm, but this will not be backported.
/ in patterns — \ is treated as an escape character\ in paths is automatically matched against ///?/C:/..., //Server/Share/...) receive special handlingreferences/optimization.md共 1 个版本