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

Michael Lewis picture Michael Lewis · Jun 14, 2014 · Viewed 14.2k times · Source

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

I keep seeing:

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

Yet, I can't seem to find an exhaustive list of the syntax/usage. These tests might 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-glob style 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?

Answer

Ilia Barahovski picture Ilia Barahovski · Oct 22, 2014

First of all, I have never worked with node-glob or minimatch libraries. But probably I can still help. There's kind of known syntax 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).

In short, IMHO for 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

Hence to your example:

  • path/* - all files and folders in path not recoursive
  • path/** - everything in path recoursively
  • path/*.* - all files and folders with point in name; matches a.txt, .hidden, noextension., folder.out, ...

From minimatch documentation - 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.