Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 961
Michael Lewis Avatar asked Jun 14 '14 16:06

Michael Lewis


People also ask

How do I use glob in node JS?

The glob pattern is most commonly used to specify filenames, called wildcard characters, and strings, called wildcard matching. The glob pattern adds all files within a directory with the . java extension, while the git add * command will add all files with the exception of those with a dot .

How do I use a wildcard in node JS?

Usage. var glob = require("glob") // options is optional glob("**/*. js", options, function (er, files) { // files is an array of filenames. // If the `nonull` option is set, and nothing // was found, then files is ["**/*. js"] // er is an error object or null. })

What is wildcard in node JS?

Wildcard-match takes one or more basic glob patterns, compiles them into a RegExp and returns a function for matching strings with it. Glob patterns are strings that contain ? , * and ** wildcards.

How do I read a directory in node JS?

The fs. readdir() method is used to asynchronously read the contents of a given directory. The callback of this method returns an array of all the file names in the directory. The options argument can be used to change the format in which the files are returned from the method.


1 Answers

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.

like image 58
Ilia Barahovsky Avatar answered Oct 05 '22 10:10

Ilia Barahovsky