JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6301884
  • Score
    100M100P100Q197027F
  • License MIT

Transform globs into RegExp

Package Exports

  • globrex

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (globrex) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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'); // true
re.test('pluck'); // true
re.test('puck'); // true

const r2 = globrex('*.min.js');
r2.test('http://example.com/jquery.min.js'); // true
r2.test('http://example.com/jquery.min.js.map'); // false

const r3 = globrex('*/www/*.js');
r3.test('http://example.com/ww/app.js'); // true
r3.test('http://example.com/www/lib/factory-proxy-model-observer.js'); // true

// Extended globs
const r4 = globToRegExp('*/www/{*.js,*.html}', { extended: true });
r4.test('http://example.com/www/app.js'); // true
r4.test('http://example.com/www/index.html'); // true

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