Globrex
Transform glob strings into RegExp objects.
Turn a *
-wildcard style glob ("*.min.js"
) into a JavaScript regular expression object (/^.*\.min\.js$/
).
Installation
npm install globrex
Usage
const globrex = require('globrex');
const r1 = globrex('p*uck');
re.test('pot luck');
re.test('pluck');
re.test('puck');
const r2 = globrex('*.min.js');
r2.test('http://example.com/jquery.min.js');
r2.test('http://example.com/jquery.min.js.map');
const r3 = globrex('*/www/*.js');
r3.test('http://example.com/ww/app.js');
r3.test('http://example.com/www/lib/factory-proxy-model-observer.js');
const r4 = globToRegExp('*/www/{*.js,*.html}', { extended: true });
r4.test('http://example.com/www/app.js');
r4.test('http://example.com/www/index.html');
API
This package support all advanced features from extglob
.
To lean more about globbing look here:
globrex(glob, opts)
Type: function
Returns: RegExp
Transform a glob string into an JavaScript RegExp
object.
Returns a new RegExp
instance.
Note: Read more about how to use RegExp.on MDN.
glob
Type: String
Glob string to transform.
opts
Type: Object
Default: { extended: false, globstar: false, strict: false, flags: '' }
Configuration object, that enables/disable different features.
opts.extended
Type: Boolean
Default: false
Whether we are matching so called "extended" globs (like bash) and should
support single character matching, matching ranges of characters, group
matching, etc.
Note: Interprets [a-d]
as [abcd]
. To match a literal -
, include it as first or last character.
opts.globstar
Type: Boolean
Default: false
When globstar is false
the '/foo/*'
is translated a regexp like
'^\/foo\/.*$'
which will match any string beginning with '/foo/'
.
When globstar is true
, '/foo/*'
is translated to regexp like
'^\/foo\/[^/]*$'
which will match any string beginning with '/foo/'
BUT
which does not have a '/'
to the right of it.
E.g. with '/foo/*'
these will match: '/foo/bar'
, '/foo/bar.txt'
but
these will not '/foo/bar/baz'
, '/foo/bar/baz.txt'
Note: When globstar is true
, '/foo/**'
is equivelant to '/foo/*'
when globstar is false
.
opts.strict
Type: Boolean
Default: false
Don't be so strict.
Be forgiving about ///
and make everything after the first /
optional.
This is how bash-globbing works.
opts.flags
Type: String
Default: ''
RegExp flags (eg "i"
) to pass in to RegExp constructor.
opts.pathsegments
Type: Boolean
Default: false
If true, returns object with both the full RegExp
object AND a segments
array contaiing a RegExp
object
for each segment in a path.
Example return value could be:
{
segments: [ /^foo$/, /^bar$/, /^([^\/]*)$/, '^baz\\.(md|js|txt)$' ],
full: /^foo\/bar\/.*\/baz\.\{md\,js\,txt\}$/
}
Note: This only makes sense for POSIX paths like /foo/bar/hello.js - not globbing on regular strings.
License
MIT © Terkel Gjervig