Does String match glob pattern

坚强是说给别人听的谎言 提交于 2021-01-02 08:20:30

问题


I have an array of paths, let's say:

/Users/alansouza/workspace/project/src/js/components/chart/Graph.js

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?

For example:

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

if (matches) {
  //do something
}

FYI: I'm using https://github.com/isaacs/node-glob


回答1:


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

it looks like node glob is another option.
https://github.com/isaacs/node-glob

And node has its own glob implementation
https://github.com/isaacs/minimatch

My first thought was to see if phpjs had implemented a glob function but it looks like they haven't :(
http://locutus.io/php/




回答2:


As @chiliNUT theorized in a comment, minimatch has this behaviour:

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


来源:https://stackoverflow.com/questions/41112143/does-string-match-glob-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!