asishkhuntia
asishkhuntia

Reputation: 427

How to include multiple file extensions with 'glob.sync' in NodeJS?

Can anyone please suggest how to add multiple file extensions with the glob.sync method.

Something like:

const glob = require('glob');
let files = glob.sync(path + '**/*.(html|xhtml)');

Thank you :)

Upvotes: 24

Views: 23815

Answers (1)

robertklep
robertklep

Reputation: 203241

You can use this (which most shells support as well):

glob.sync(path + '**/*.{html,xhtml}')

Or this one:

glob.sync(path + '**/*.@(html|xhtml)')

EDIT: I initially also suggested this pattern:

glob.sync(path + '**/*.+(html|xhtml)')

However, this will also match files that have .htmlhtml as extension (plus any other combination of html and xhtml, in single or multiple occurrences), which is incorrect.

Upvotes: 36

Related Questions