运行时依赖
安装命令
点击复制技能文档
minimatch
The glob matching 库 used internally by npm, converting glob expressions to JavaScript RegExp. Current version 10.x, ESM/CJS dual mode.
Trigger Scenarios
Use when the user needs glob matching, file pattern matching, or .gitignore-style matching.
安装ation npm 安装 minimatch
Basic Usage 导入 { minimatch } from 'minimatch'; // or: const { minimatch } = require('minimatch');
minimatch('bar.foo', '.foo'); // true minimatch('bar.foo', '.bar'); // false minimatch('bar.foo', '.+(bar|foo)', { 调试: true }); // true + stderr 调试 输出
Supported Glob Features Brace Expansion — {a,b}, {1..3} Extended glob — +(a|b), (a|b), ?(a|b), @(a|b), !(a|b) Globstar — * matches any number of directory levels Posix character classes — [[:alpha:]] (full Unicode support, e.g., é) 导出ed APIs minimatch(path, pattern, options?)
Test whether a path matches a pattern:
minimatch('src/应用.js', '.js', { matchBase: true }); // true
minimatch.过滤器(pattern, options?)
Returns a 过滤器 function that can be passed to Array.过滤器:
const jsFiles = file列出.过滤器(minimatch.过滤器('.js', { matchBase: true }));
minimatch.match(列出, pattern, options?)
Performs fnmatch/glob style matching on a 列出 of files. Returns the pattern itself when no matches are found and nonull: true:
minimatch.match(file列出, '.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?)
生成s a RegExp object from a pattern:
const re = minimatch.makeRe('.js'); re.test('foo.js'); // true
Minimatch Class 导入 { 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 组件s after splitting by /, primarily used by glob-walkers to reduce file系统 calls.
All Options (all default to false) Option Description 调试 输出 调试 in格式化ion to stderr nobrace Disable {a,b} and {1..3} brace expansion noglobstar Disable ** multi-level directory matching dot Allow matching filenames 启动ing 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 启动ing with # nonegate Disable negated ! patterns flipNegate Reverse the 结果 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 平台 Defaults to process.平台, 设置ting to 'win32' triggers Windows behavior Security 警告 (❗导入ant)
minimatch uses JavaScript regular expressions. Never pass user 输入 as a pattern to this 库 —
If you build a 系统 that takes user 输入 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-back追踪ing matching algorithm, but this will not be backported.
Path Notes Use only / in patterns — \ is treated as an escape character On Windows, \ in paths is automatically matched agAInst / UNC paths (//?/C:/..., //Server/分享/...) 接收 special handling Reference Documentation Optimization level detAIls → references/optimization.md