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

1

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

Stafani answered 30/3, 2017 at 12:42 Comment(0)
W
36

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.

Worcester answered 30/3, 2017 at 12:54 Comment(2)
What is the difference between the both?Murial
@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).Worcester

© 2022 - 2024 — McMap. All rights reserved.