字符串是否匹配全局模式 [英] Does String match glob pattern
问题描述
我有一系列的路径,比如说:
I have an array of paths, let's say:
/Users/alansouza/workspace/project/src/js/components/chart/Graph.js
此外,我在配置文件中还有一个条目,该条目具有通配符(glob)格式的此路径的其他属性,例如:
Also I have an entry in a configuration file with additional attributes for this path in a wildcard(glob) format, as in:
{
'**/*.js': {
runBabel: true
}
}
问题:是否有一种简单的方法来验证路径是否与通配符表达式匹配?
Question: Is there an easy way to validate if the path matches the wildcard expression?
例如:
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
FYI: I'm using https://github.com/isaacs/node-glob
推荐答案
您可以将glob转换为带有节点 glob-to-regex
的正则表达式,然后根据该正则表达式验证路径字符串.br/> https://www.npmjs.com/package/glob-to-regexp
You could convert the glob to to a regex with node glob-to-regex
and then validate path strings against the regex.
https://www.npmjs.com/package/glob-to-regexp
它似乎是 node glob
的另一种选择.
https://github.com/isaacs/node-glob
it looks like node glob
is another option.
https://github.com/isaacs/node-glob
并且节点具有自己的glob实现
https://github.com/isaacs/minimatch
And node has its own glob implementation
https://github.com/isaacs/minimatch
我的第一个想法是看 phpjs
是否实现了glob函数,但看起来它们还没有:( <> http://locutus.io/php/
My first thought was to see if phpjs
had implemented a glob function but it looks like they haven't :(
http://locutus.io/php/
这篇关于字符串是否匹配全局模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!