https://github.com/terkelg/globrex
Glob to regular expression with support for extended globs.
https://github.com/terkelg/globrex
glob glob-to-regex globbing pattern regex regexp regular-expression wildcard
Last synced: about 10 hours ago
JSON representation
Glob to regular expression with support for extended globs.
- Host: GitHub
- URL: https://github.com/terkelg/globrex
- Owner: terkelg
- License: mit
- Created: 2018-04-10T06:08:55.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-09-06T22:15:05.000Z (about 1 year ago)
- Last Synced: 2025-04-02T04:03:04.439Z (6 months ago)
- Topics: glob, glob-to-regex, globbing, pattern, regex, regexp, regular-expression, wildcard
- Language: JavaScript
- Homepage:
- Size: 85.9 KB
- Stars: 72
- Watchers: 4
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
![]()
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
```js
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: `Object`Transform globs into regular expressions.
Returns object with the following properties:#### regex
Type: `RegExp`
JavaScript `RegExp` instance.
> **Note**: Read more about how to use [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) on MDN.
#### path
This property only exists if the option `filepath` is true.
> **Note:** `filepath` is `false` by default
#### path.segments
Type: `Array`
Array of `RegExp` instances seperated by `/`.
This can be usable when working with file paths or urls.Example array could be:
```js
[ /^foo$/, /^bar$/, /^([^\/]*)$/, '^baz\\.(md|js|txt)$' ]
```#### path.regex
Type: `RegExp`
JavaScript `RegExp` instance build for testign against paths.
The regex have different path seperators depending on host OS.### 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.filepath
Type: `Boolean`
Default: `false`Parse input strings as it was a file path for special path related features. This feature only makes sense if the input is a POSIX path like `/foo/bar/hello.js` or URLs.
When `true` the returned object will have an additional `path` object.
- `segment`: Array containing a `RegExp` object for each path segment.
- `regex`: OS specific file path `RegExp`. Path seperator used is based on the operating system.
- `globstar`: Regex string used to test for globstars.> **Note: Please only use forward-slashes in file path glob expressions**
> Though windows uses either `/` or `\` as its path separator, only `/`
> characters are used by this glob implementation. You must use
> forward-slashes **only** in glob expressions. Back-slashes will always
> be interpreted as escape characters, not path separators.## References
Learn more about advanced globbing here
- [mywiki.wooledge.org/glob](http://mywiki.wooledge.org/glob)
- [linuxjournal](http://www.linuxjournal.com/content/bash-extended-globbing)## License
MIT © [Terkel Gjervig](https://terkel.com)