String是否匹配glob模式

Ala*_*uza 4 javascript regex glob wildcard

我有一系列路径,让我们说:

/Users/alansouza/workspace/project/src/js/components/chart/Graph.js
Run Code Online (Sandbox Code Playgroud)

此外,我在配置文件中有一个条目,其中包含通配符(glob)格式的此路径的其他属性,如:

{
  '**/*.js': {
    runBabel: true
  }
}
Run Code Online (Sandbox Code Playgroud)

问题:是否有一种简单的方法来验证路径是否与通配符表达式匹配?

例如:

const matches = Glob.matches(
  '**/*.js',
  '/Users/alansouza/workspace/project/src/js/components/chart/Graph.js'
);

if (matches) {
  //do something
}
Run Code Online (Sandbox Code Playgroud)

仅供参考:我正在使用https://github.com/isaacs/node-glob

chi*_*NUT 7

您可以将glob转换为带有节点的正则表达式,glob-to-regex然后针对正则表达式验证路径字符串.
https://www.npmjs.com/package/glob-to-regexp

它看起来node glob是另一种选择.
https://github.com/isaacs/node-glob

节点有自己的glob实现
https://github.com/isaacs/minimatch

我的第一个想法是看看是否phpjs实现了一个glob函数,但看起来他们没有:(
http://locutus.io/php/

  • 好吧,我自己正在使用 node-glob。但它没有我正在寻找的“匹配”支持。 (2认同)

AJ *_*son 5

正如@chiliNUT 在评论中理论化的那样,minimatch具有以下行为:

minimatch("file.js", "**/*.js") // true/false
minimatch.match(["file1.js", "file2.js"], "**/*.js") // array of matches
Run Code Online (Sandbox Code Playgroud)