24

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 :)

Flip
  • 6,233
  • 7
  • 46
  • 75
asishkhuntia
  • 417
  • 1
  • 4
  • 7

1 Answers1

35

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.

robertklep
  • 198,204
  • 35
  • 394
  • 381
  • What is the difference between the both? – Jai Saravanan Apr 04 '23 at 10:34
  • @JaiSaravanan `@(PATTERN)` matches files that have `PATTERN` in their name _exactly once_, `+(PATTERN)` matches files that have `PATTERN` in their name _at least once_ (so my example of `*.+(html|xhtml)` also matches files that end with `.htmlhtml`, which is incorrect). – robertklep Apr 04 '23 at 11:37