Node-glob Learning

Source: Internet
Author: User
Tags glob

Node's Glob module allows you to write a glob rule using * and other symbols, like in the shell, to get files that match the corresponding rules.

This Glob tool is based on JavaScript. It uses the Minimatch library to match


First download the Glob package:

NPM Install Glob

Call Format:

Var glob = require("glob")

// options are optional
Glob("**/*.js", options, function (er, files) {
   // files is an array of matching files.
   // If the `nonull` option is set to true and no files are found, then files is the glob rule itself, not an empty array.
   // er is the error encountered in the process of finding

"Globs" is a model, such as when you enter LS *.js in the command line, or you write bulid/* in the. gitignore file.

When parsing a path model, the contents of the curly braces separated by commas are expanded, and the part can contain "/", such as A{/B/C, BCD, which will be expanded into a/b/c and ABCD

A segment of the path can be represented by the following characters, each of which has a very cool effect:

  • 1. *: Match 0 or more arbitrary characters in the path segment:
  • //*: Match a part of the path: 0 or more characters
    Glob("js/*.js",function (er, files) {

    Get all the JS files in the JS directory. (not included with '. ') Files that begin with)

  • 2.? : Matches 1 arbitrary characters in the path segment:
  • //?: Match some part of the path: 1 character
    Glob("js/?.js",function (er, files) {

    Get all the names in JS directory with only 1 characters of JS.

  • 3. [...]: matches the character within the specified range in the path segment:
    Note cannot be combined, only one of the characters
  • //[]: Match a part of the path: the specified range
    Glob("js/a[0-3].js",function (er, files) {

    Get JS directory under a beginning, the second character is between 0-3 (including 0 and 3) of JavaScript (A03.js cannot be matched to)

  • 4. * (Pattern|pattern|pattern): matches 0 or more or any combination of multiple models in parentheses
    Note | There can be no spaces before and after
  • //*(pattern|pattern|pattern): Match some part of the path: 0 or more of multiple models.
    / / In addition to the three models themselves, if it is a combination, such as ab.js, but only a model is not acceptable, such as a4.js.
    Glob("js/*(a|a1|b).js",function (er, files) {

    Get JS directory under A.js,a1.js,b.js, or a,a1,b a combination of these characters JS, such as Ab.js

  • 5.! (Pattern|pattern|pattern): Match does not contain any model
    Need attention:! (pattern|pattern|pattern) Not equal! (* (Pattern|pattern|pattern))
  • //!(pattern|pattern|pattern): Match some part of the path: Does not contain any models.
    //With a or b, it is excluded. It should be noted that it is not the negation of *(a|b)
    Glob("js/!(a|b).js",function (er, files) {

    Gets the JS directory under the name does not contain a, also does not contain all files of B.

  • 6.? (Pattern|pattern|pattern): matches 0 or any 1 in multiple models.
  • The difference between it and 4 is that it cannot be combined. Must match exactly

  • //?(pattern|pattern|pattern): Match some part of the path: 0 or 1 of multiple models.
    / / Accurately match the model, can not be combined.
    Glob("js/?(a|a2|b).js",function (er, files) {

    Get JS directory under A.js,a2.js,b.js

  • 7. + (Pattern|pattern|pattern): matches 1 or more of multiple models.
    The difference between it and 4 is that there must be an empty mismatch
  • //+(pattern|pattern|pattern): Match some part of the path: one or more of the multiple models.
    // can be any model, or a combination of them, such as ab.js
    Glob("js/+(a|a1|b).js",function (er, files) {

    Get JS directory under A.js,a1.js,b.js, or a,a1,b a combination of these characters JS, such as Ab.js

  • 8. @ (Pattern|pat*|pat?ern): matches any 1 of multiple models.
  • @(pattern|pattern|pattern): Match some part of the path: 1 of multiple models.
    / / Accurately match the model, can not be combined. And the difference between the difference is that it can not be empty. Must be one of them.
    Glob("js/@(a|a1|b).js",function (er, files) {

    The difference between the and 6 is that the mismatch is empty for the case

  • 9. * *: As with 1, any content can be matched, but * * not only matches a segment in the path, but also matches the '/' content of ' a/b/c ', so it can also match files under subfolders.
  • //**: Not a part of a separate path, but with ‘/‘, so all current folders and subfolders match
    Glob("**/@(a|a1|b).js",function (er, files) {

    Get a.js,a1.js,b.js under All folders and subfolders of the current directory
    Another way to do this is to set the Matchbase property to True, which can also be used to search for all subfolders under the current path:

  • //matchBase: After setting to true, look for matching files in all folders and subfolders in the current directory.
    Glob("@(a|a1|b).js",{matchBase:true},function (er, files) {

No matching files were obtained:

When Glob does not get any matching files, and does not return the model itself as in the shell, the files parameter returns an empty array, and if you need to let files return the model itself, you need to set the Nonull property to True

//nonull: After setting to true, if no matching file is found, no empty string is returned, but the original glob statement is returned.
Glob("@(c|d|e).js",{nonull:true},function (er, files) {

To get a list of matching files synchronously:

All that is said is asynchronous, passing in a callback that executes the callback when it gets to the matching file. If you need to synchronize the Get file list, you can do this:

var files = Glob.sync (pattern, [options])

Glob class:

By instantiating a glob. Glob class, you can get a Glob object.

var Glob = require("glob").Glob
var mg = new Glob(pattern, options, cb)

The arguments passed in when instantiated are the same as Glob (PATTERN,OPTIONS,CB).

It can get a return value, and this return value is a eventemitter.

If you set the Sync property to True in the options, it means that synchronization gets. The CB callback cannot be passed in. To get the matching result, you can get it by G.found:

var globInstance = new glob.Glob("@(a|a1|b).js",{nonull:true,matchBase:true,sync:true});


    • The End:end event is triggered at the end of the file match, when all matching results are found, and the parameters that it accepts are the array of files
    • The Match:match event is triggered every time a file is matched, and the parameters it accepts are the matching files.
    • The Error:error event is triggered when a match encounters an error. The accepted parameter is the error message
    • Abort: The Abort event is triggered when the instance calls the. Abort () method


    • pausePause Match Search
    • resumeContinue matching Search
    • abortStop matching search forever, can't continue

var globInstance = new glob.Glob("js/@(a|a1|b).js",{nonull:true});


    • minimatchThe Minimatch object used by the Glob.
    • optionsOptions that are passed to the function.
    • abortedabort()after the function has been called, its value is true.
    • cache
    • Statcache
    • Symlinks
    • Realpathcache

Options option:

Options are used to configure how matches are matched when the model is matched. All parameters that can be passed into the minimatch can also be passed to Glob, and Node-glob has added some additional configuration items.

All options if there is no special description, the default value is False

All options also apply to the Glob class.

    • cwd 
    • root 
    • dot 
    • Nomount 
    • Mark
    • nosort 
    • stat 
    • silent 
    • strict 
    • cache
    • statcache 
    • symlinks 
    • sync 
    • nounique 
    • nonull 
    • debug
    • nobrace
    • noglobstar 
    • noext
    • nocase 
    • matchbase 
    • nonull 
    • nodir 
    • ignore 
    • follow 
    • realpath 
    • nonegate 
    • nocomment

Reference Original: Https://

Transferred from:

Node-glob Learning

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.