Search code examples
javascriptnode.jsglob

Glob string comparison


I have a glob string, and a file path, is it possible to test the file path to see if the string matches the glob?

let path = ['/home/location/file.scss','/home/location/file.cat'];
let pattern = '**/*.{sass,scss}';

path.forEach(file => {
    if(/* Test success */) {
        // read file and process it's content
    }
});

Solution

  • Can you use regex? Because you could convert the pattern to regex as /\.s[ac]ss$/i, and then do

    path.forEach(file => {
      if (file.match(pattern)) {
      }
    });
    

    There's also glob-to-regexp, in the case you have to write the pattern in glob.