Nodejs glob multiple patterns

Viewed 3327

Is it possible to include multiple patterns in a single search string in glob for nodejs? Like i need to find all files that have "abc.pdf" and "xyz.pdf".

2 Answers

When using node-glob you can provide multiple patterns like this:

 "*(pattern1|pattern2|...)"

Which would translate in your example like:

 "*(abc.pdf|xyz.pdf)"

Full example (find all .html and .js files in current directory):

glob("*(*.js|*.html)", {}, function (err, files) {
   console.log(files)
})

For who want a glob options with recursive matching with multiple file extension. This will match all file within the path folder that has extension .ts?x and .js?x.

import * as glob from "glob";
// Synchronous operation
glob.sync(`path/**/*(*.ts|*.tsx|*.js|*.jsx)`, {...globOptions});
// Asynchronous operation
glob(`path/**/*(*.ts|*.tsx|*.js|*.jsx)`, {...globOptions}, () => {});
Related