String 是否匹配 glob 模式
Does String match glob pattern
我有一组路径,比方说:
/Users/alansouza/workspace/project/src/js/components/chart/Graph.js
此外,我在配置文件中有一个条目,其中包含通配符(glob)格式的此路径的附加属性,如:
{
'**/*.js': {
runBabel: true
}
}
问题:是否有简单的方法来验证路径是否匹配通配符表达式?
例如:
const matches = Glob.matches(
'**/*.js',
'/Users/alansouza/workspace/project/src/js/components/chart/Graph.js'
);
if (matches) {
//do something
}
仅供参考:我正在使用 https://github.com/isaacs/node-glob
您可以将 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/
正如@chiliNUT 在评论中提出的理论,minimatch
具有以下行为:
minimatch("file.js", "**/*.js") // true/false
minimatch.match(["file1.js", "file2.js"], "**/*.js") // array of matches
我有一组路径,比方说:
/Users/alansouza/workspace/project/src/js/components/chart/Graph.js
此外,我在配置文件中有一个条目,其中包含通配符(glob)格式的此路径的附加属性,如:
{
'**/*.js': {
runBabel: true
}
}
问题:是否有简单的方法来验证路径是否匹配通配符表达式?
例如:
const matches = Glob.matches(
'**/*.js',
'/Users/alansouza/workspace/project/src/js/components/chart/Graph.js'
);
if (matches) {
//do something
}
仅供参考:我正在使用 https://github.com/isaacs/node-glob
您可以将 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/
正如@chiliNUT 在评论中提出的理论,minimatch
具有以下行为:
minimatch("file.js", "**/*.js") // true/false
minimatch.match(["file1.js", "file2.js"], "**/*.js") // array of matches