Craft By Zen

📖 2 min read

🔖 learning

What The Glob

I ran across the node-glob and realized I didn’t know what a glob is. I read through the node-glob documentation and found out globs are a form of pattern matching. I realized I had been using globs for a long time, like the wildcard notation, without knowing its name.

adventure time glob
adventure time glob

Wildcards

There are generally two wildcards you can use for glob functions.

  1. * - “any string of characters”
  2. ? - one character

There are also brackets, where the character must be one of the following, or given as a range.

  1. [abc] - one of “a”, “b”, or “c”
  2. [a-z] - between the range of “a” to “z”.

You can also line this up with a ”!” to mean not in the following bracket.

  1. [!abc] - not one of “a”, “b”, or “c”
  2. [!a-z] - not in the range of “a” to “z”

The examples above are used in UNIX-based systems. These commands are slightly different in Window’s Powershell and SQL. For more information about those systems, you can read the Wikipedia article about globs.

Node Example

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.
});

Written by Jeremy Wong and published on .