Node/JavaScript glob 文件/路径匹配语法、通配符等

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24222093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:23:39  来源:igfitidea点击:

Node/JavaScript glob file/path matching syntax, wildcards, etc

javascriptnode.jsglob

提问by Michael Lewis

I just found http://gruntjs.com/configuring-tasks#globbing-patterns, which is the most helpful reference I've found.

我刚刚找到了http://gruntjs.com/configuring-tasks#globbing-patterns,这是我找到的最有用的参考资料。

I keep seeing:

我一直看到:

For more on glob pattern syntax, see the node-glob and minimatch documentation.

有关 glob 模式语法的更多信息,请参阅 node-glob 和 minimatch 文档。

Yet, I can't seem to find an exhaustive list of the syntax/usage. These testsmight be the best reference, yet still not particularly easy to decipher.

然而,我似乎找不到语法/用法的详尽列表。 这些测试可能是最好的参考,但仍然不是特别容易破译。

It seems I must be missing some critical source of documentation.

看来我一定缺少一些重要的文档来源。

I'm wondering the differences between:

我想知道以下之间的区别:

path
path/
path/*
path/*.*
path/**
path/**/
path/**/*
path/**/*.*

and any other important variations that are related that I might have omitted. I'm guessing this applies differently when doing a node-globstyle matching ('public/**/*.*') and a .gitignore(node_modules), because in the former, you need to explicitly include everything, many layers deep, and in gitignore, this is handled automatically by ignoring any directory. Is this correct?

以及我可能忽略的任何其他相关的重要变化。我猜这在进行node-glob样式匹配 ( 'public/**/*.*') 和.gitignore( node_modules)时应用不同,因为在前者中,您需要显式包含所有内容,很深,而在 gitignore 中,这是通过忽略任何目录自动处理的。这个对吗?

回答by Ilia Barahovski

First of all, I have never worked with node-globor minimatchlibraries. But probably I can still help. There's kind of knownsyntax for glob pattern matching, but frankly, a quick search in Google shows nothing short and clear. Probably this - http://hgbook.red-bean.com/read/file-names-and-pattern-matching.html#id381184- is the best resource I've found. The article in Wikipedia is exhaustive and not readable - http://en.wikipedia.org/wiki/Glob_(programming).

首先,我从来没有工作过node-globminimatch库。但也许我仍然可以提供帮助。glob 模式匹配有一些已知的语法,但坦率地说,在 Google 中的快速搜索没有显示任何简短和清晰的内容。可能这个 - http://hgbook.red-bean.com/read/file-names-and-pattern-matching.html#id381184- 是我找到的最好的资源。维基百科中的文章详尽且不可读 - http://en.wikipedia.org/wiki/Glob_(programming)

In short, IMHO for node-glob:

简而言之,恕我直言,node-glob

  • *- stands for any number of characters for a filename, but can't stand for /
  • **- same as *but crosses folder boundaries
  • [abxy]- can replace any one character from a list; [0-9]can stand for any number
  • *- 代表文件名的任意数量的字符,但不能代表 /
  • **- 相同*但跨越文件夹边界
  • [abxy]- 可以替换列表中的任何一个字符;[0-9]可以代表任何数字

Hence to your example:

因此,对于您的示例:

  • path/*- all files and folders in pathnot recoursive
  • path/**- everything in pathrecoursively
  • path/*.*- all files and folders with point in name; matches a.txt, .hidden, noextension., folder.out, ...
  • path/*- 所有文件和文件夹都path不可递归
  • path/**- 一切都在path递归
  • path/*.*- 所有带有点名的文件和文件夹;匹配a.txt, .hidden, noextension., folder.out, ...

From minimatchdocumentation - https://github.com/isaacs/minimatch, - it does the same, but utilizes richer and slightly more difficult syntax of Regular Expressions. You may look here for a comprehesive reference - http://www.w3schools.com/js/js_regexp.asp. In short, path/.*stands for anything below the path, but it's not clear if recursive or not. You may probably test it.

来自minimatch文档 - https://github.com/isaacs/minimatch, - 它的作用相同,但使用了更丰富、更困难的正则表达式语法。您可以在此处查看综合参考 - http://www.w3schools.com/js/js_regexp.asp。简而言之,path/.*代表路径之下的任何东西,但不清楚是否递归。你可能会测试它。