Skip to content
/ globrex Public
forked from terkelg/globrex

Glob to regular expression with support for extended globbing

License

Notifications You must be signed in to change notification settings

lukeed/globrex

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

globrex

globrex

Simple but powerful glob to regular expression compiler.

Install

npm install globrex --save

Core Features

  • extended globbing: transform advance ExtGlob features
  • simple: no dependencies
  • paths: split paths into multiple RegExp segments

Usage

const globrex = require('globrex');

const result = globrex('p*uck')
// => { regex: /^p.*uck$/, string: '^p.*uck$', segments: [ /^p.*uck$/ ] }

result.regex.test('pluck'); // true

API

globrex(glob, options)

Type: function
Returns: { regex, string, segments }

Transform globs intp regular expressions. Returns object with the following properties:

regex

Type: RegExp

JavaScript RegExp instance.

Note: Read more about how to use RegExp on MDN.

string

Type: String

Regex string representation of the glob.

segments

Type: Array

Array of RegExp instances seperated by /. This can be usable when working with paths or urls.

Example array could be:

[ /^foo$/, /^bar$/, /^([^\/]*)$/, '^baz\\.(md|js|txt)$' ]

Note: This only makes sense for POSIX paths like /foo/bar/hello.js or URLs. Not globbing on regular strings.

glob

Type: String

Glob string to transform.

options.extended

Type: Boolean
Default: false

Enable all advanced features from extglob.

Matching so called "extended" globs pattern like 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.

options.globstar

Type: Boolean
Default: false

When globstar is false globs like '/foo/*' are transformed to the following '^\/foo\/.*$' which will match any string beginning with '/foo/'.

When the globstar option is true, the same '/foo/*' glob is transformed to '^\/foo\/[^/]*$' which will match any string beginning with '/foo/' that does not have a '/' to the right of it. '/foo/*' will match: '/foo/bar', '/foo/bar.txt' but not '/foo/bar/baz' or '/foo/bar/baz.txt'.

Note: When globstar is true, '/foo/**' is equivelant to '/foo/*' when globstar is false.

options.strict

Type: Boolean
Default: false

Be forgiving about mutiple slashes, like /// and make everything after the first / optional. This is how bash glob works.

options.flags

Type: String
Default: ''

RegExp flags (e.g. 'i' ) to pass to the RegExp constructor.

options.windows

Type: Boolean
Default: System OS

Split segment as a windows path, otherwise splut as unix. Defaults to the OS running the package.

References

Learn more about advanced globbing here

License

MIT © Terkel Gjervig

About

Glob to regular expression with support for extended globbing

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%