glob-to-regexp

中文文档
0.4.1发布于 2019/04/02BSD-2-ClauseCommonJS零依赖近 30 天下载 1.8亿

Convert globs to regular expressions

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

Glob To Regular Expression

Build Status

Turn a *-wildcard style glob ("*.min.js") into a regular expression (/^.*\.min\.js$/)!

To match bash-like globs, eg. ? for any single-character match, [a-z] for character ranges, and {*.html, *.js} for multiple alternatives, call with { extended: true }.

To obey globstars ** rules set option {globstar: true}. NOTE: This changes the behavior of * when globstar is true as shown below: When {globstar: true}: /foo/** will match any string that starts with /foo/ like /foo/index.htm, /foo/bar/baz.txt, etc. Also, /foo/**/*.txt will match any string that starts with /foo/ and ends with .txt like /foo/bar.txt, /foo/bar/baz.txt, etc. Whereas /foo/* (single *, not a globstar) will match strings that start with /foo/ like /foo/index.htm, /foo/baz.txt but will not match strings that contain a / to the right like /foo/bar/baz.txt, /foo/bar/baz/qux.dat, etc.

Set flags on the resulting RegExp object by adding the flags property to the option object, eg { flags: "i" } for ignoring case.

Install

npm install glob-to-regexp

Usage

var globToRegExp = require('glob-to-regexp');
var re = globToRegExp("p*uck");
re.test("pot luck"); // true
re.test("pluck"); // true
re.test("puck"); // true

re = globToRegExp("*.min.js");
re.test("http://example.com/jquery.min.js"); // true
re.test("http://example.com/jquery.min.js.map"); // false

re = globToRegExp("*/www/*.js");
re.test("http://example.com/www/app.js"); // true
re.test("http://example.com/www/lib/factory-proxy-model-observer.js"); // true

// Extended globs
re = globToRegExp("*/www/{*.js,*.html}", { extended: true });
re.test("http://example.com/www/app.js"); // true
re.test("http://example.com/www/index.html"); // true

License

Copyright (c) 2013, Nick Fitzgerald

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

版本与发布记录

glob-to-regexp 共发布 8 个版本,首次发布于 2013/07/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.4.1latest2019/04/027 年前
0.4.02018/02/108 年前
0.3.02016/08/2810 年前
0.2.02016/08/2810 年前
0.1.02015/07/1511 年前
0.0.22015/01/3111 年前

关于 glob-to-regexp 的常见问题

glob-to-regexp 是什么?

Convert globs to regular expressions

如何安装 glob-to-regexp?

在项目目录运行 npm install glob-to-regexp,或使用 pnpm add / yarn add / bun add。

glob-to-regexp 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/glob-to-regexp 是否存在。

glob-to-regexp 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

glob-to-regexp 可以商用吗?

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

glob-to-regexp 的下载量有多少?

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

glob-to-regexp 还在维护吗?

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