glob
glob lets you convert glob patterns into regular expressions.
It has no dependencies and is 1120 bytes, or 471 bytes gzipped.
It works in all browsers and all versions of Node.
glob is a code-golfing fork of the amazing globrex.
Usage
Add glob to your project:
npm install @jsxtools/glob
Use glob to convert glob patterns into regular expressions:
glob = require('@jsxtools/glob')
match1 = glob('path/to/{*,*/*}.js')
match1.test('path/to/file.js')
match1.test('path/to/lib/file.js')
match1.test('path/to/file.js.map')
match1.test('path/to/lib/deeper/file.js')
match1.test('path/to/file.js/lib')
match2 = glob('p[a-z]ck')
match2.test('pack')
match2.test('puck')
match2.test('pck')
match2.test('pluck')
match2.test('p-ck')
glob supports single character matching.
glob('/path/to/file?.txt').test('/path/to/file1.txt')
glob('/path/to/file?.txt').test('/path/to/file2.txt')
glob('/path/to/file?.txt').test('/path/to/file3.txt')
glob('/path/to/file?.txt').test('/path/to/fileZ.txt')
glob('/path/to/file?.txt').test('/path/to/file11.txt')
glob('/path/to/file?.txt').test('/path/to/file.txt')
glob supports matching ranges of characters.
glob('/path/to/[a-c]').test('/path/to/a')
glob('/path/to/[a-c]').test('/path/to/b')
glob('/path/to/[a-c]').test('/path/to/c')
glob('/path/to/[a-c]').test('/path/to/d')
glob('/path/to/[a-c]').test('/path/to/e')
glob supports group matching.
glob('{*.doc,*.pdf}').test('file.doc')
glob('{*.doc,*.pdf}').test('file.pdf')
glob('{*.doc,*.pdf}').test('file.xxx.pdf')
glob('{*.doc,*.pdf}').test('file.pdf.xxx')
glob supports globstar patterns.
glob('/path/*/index.js').test('/path/to/index.js')
glob('/path/*/index.js').test('/path/of/index.js')
glob('/path/*/index.js').test('/path/to/the/index.js')
glob('/path/*/index.js').test('/path/of/the/index.js')
glob('/path/*/index.js').test('/path/index.js')
glob('/path/*/index.js').test('/index.js')
glob supports posix-style paths and does not support windows-style paths.
I recommend you detect the windows environment and manage this conversion yourself:
const isWin = process.platform === 'win32'
const win2pos = win32path => win32path.replace(/^(\w+):|\\/g, '/$1')
const all2pos = anypath => isWin ? win2pos(anypath) : anypath
all2pos('/Users/THX1138/images/../files')
all2pos('C:\\Users\\THX1138\\images\\..\\files')