anymatch

中文文档
3.1.3发布于 2022/11/22ISCTypeScript近 30 天下载 4.4亿

Matches strings against configurable strings, globs, regular expressions, and/or functions

原始使用文档
文档目录3 节
  1. Usage
  2. Changelog
  3. License

anymatch Build Status Coverage Status

Javascript module to match a string against a regular expression, glob, string, or function that takes the string as an argument and returns a truthy or falsy value. The matcher can also be an array of any or all of these. Useful for allowing a very flexible user-defined config to define things like file paths.

Note: This module has Bash-parity, please be aware that Windows-style backslashes are not supported as separators. See https://github.com/micromatch/micromatch#backslashes for more information.

Usage

npm install anymatch

anymatch(matchers, testString, [returnIndex], [options])

  • matchers: (Array|String|RegExp|Function) String to be directly matched, string with glob patterns, regular expression test, function that takes the testString as an argument and returns a truthy value if it should be matched, or an array of any number and mix of these types.
  • testString: (String|Array) The string to test against the matchers. If passed as an array, the first element of the array will be used as the testString for non-function matchers, while the entire array will be applied as the arguments for function matchers.
  • options: (Object [optional]_) Any of the picomatch options.
    • returnIndex: (Boolean [optional]) If true, return the array index of the first matcher that that testString matched, or -1 if no match, instead of a boolean result.
const anymatch = require('anymatch');

const matchers = [ 'path/to/file.js', 'path/anyjs/**/*.js', /foo.js$/, string => string.includes('bar') && string.length > 10 ] ;

anymatch(matchers, 'path/to/file.js'); // true
anymatch(matchers, 'path/anyjs/baz.js'); // true
anymatch(matchers, 'path/to/foo.js'); // true
anymatch(matchers, 'path/to/bar.js'); // true
anymatch(matchers, 'bar.js'); // false

// returnIndex = true
anymatch(matchers, 'foo.js', {returnIndex: true}); // 2
anymatch(matchers, 'path/anyjs/foo.js', {returnIndex: true}); // 1

// any picomatc

// using globs to match directories and their children
anymatch('node_modules', 'node_modules'); // true
anymatch('node_modules', 'node_modules/somelib/index.js'); // false
anymatch('node_modules/**', 'node_modules/somelib/index.js'); // true
anymatch('node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // false
anymatch('**/node_modules/**', '/absolute/path/to/node_modules/somelib/index.js'); // true

const matcher = anymatch(matchers);
['foo.js', 'bar.js'].filter(matcher);  // [ 'foo.js' ]
anymatch master* ❯

anymatch(matchers)

You can also pass in only your matcher(s) to get a curried function that has already been bound to the provided matching criteria. This can be used as an Array#filter callback.

var matcher = anymatch(matchers);

matcher('path/to/file.js'); // true
matcher('path/anyjs/baz.js', true); // 1

['foo.js', 'bar.js'].filter(matcher); // ['foo.js']

Changelog

See release notes page on GitHub

License

ISC

版本与发布记录

anymatch 共发布 18 个版本,首次发布于 2013/11/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.1.3latest2022/11/223 年前
3.1.22021/04/075 年前
3.1.12019/10/017 年前
3.1.02019/08/307 年前
3.0.32019/07/077 年前
3.0.22019/05/147 年前

关于 anymatch 的常见问题

anymatch 是什么?

Matches strings against configurable strings, globs, regular expressions, and/or functions

如何安装 anymatch?

在项目目录运行 npm install anymatch,或使用 pnpm add / yarn add / bun add。

anymatch 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

anymatch 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 8。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

anymatch 可以商用吗?

anymatch 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

anymatch 的下载量有多少?

根据 npm 官方下载统计,anymatch 在 2026/10/05 – 2026/11/03 这 30 天内被下载 444,937,389 次,日均约 14,831,246 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

anymatch 还在维护吗?

最新版本 3.1.3 发布于 2022/11/22(3 年前);至今共发布 18 个版本,其中过去 12 个月发布了 0 个;首次发布于 2013/11/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。