extglob
Extended glob support for JavaScript. Adds (almost) the expressive power of regular expressions to glob patterns.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.
Install
Install with npm:
Convert an extglob string to a regex-compatible string.
More complete (and correct) support than minimatch (minimatch fails a large percentage of the extglob tests)
Handles negation patterns
Handles nested patterns
Organized code base, easy to maintain and make changes when edge cases arise
As you can see by the benchmarks, extglob doesn't pay with speed for it's completeness, accuracy and quality.
Heads up!: This library only supports extglobs, to handle full glob patterns and other extended globbing features use micromatch instead.
Usage
The main export is a function that takes a string and options, and returns an object with the parsed AST and the compiled .output
, which is a regex-compatible string that can be used for matching.
Extglob cheatsheet
Extended globbing patterns can be defined as follows (as described by the bash man page):
pattern | regex equivalent | description |
| `(... | ...)?` |
| `(... | ...)*` |
| `(... | ...)+` |
| `(... | ...)` [1] |
| N/A | Matches anything except one of the given pattern(s) |
API
Convert the given extglob
pattern into a regex-compatible string. Returns an object with the compiled result and the parsed AST.
Params
pattern
{String}options
{Object}returns
{String}
Example
Takes an array of strings and an extglob pattern and returns a new array that contains only the strings that match the pattern.
Params
list
{Array}: Array of strings to matchpattern
{String}: Extglob patternoptions
{Object}returns
{Array}: Returns an array of matches
Example
Returns true if the specified string
matches the given extglob pattern
.
Params
string
{String}: String to matchpattern
{String}: Extglob patternoptions
{String}returns
{Boolean}
Example
Returns true if the given string
contains the given pattern. Similar to .isMatch
but the pattern can match any part of the string.
Params
str
{String}: The string to match.pattern
{String}: Glob pattern to use for matching.options
{Object}returns
{Boolean}: Returns true if the patter matches any part ofstr
.
Example
Takes an extglob pattern and returns a matcher function. The returned function takes the string to match as its only argument.
Params
pattern
{String}: Extglob patternoptions
{String}returns
{Boolean}
Example
Convert the given extglob
pattern into a regex-compatible string. Returns an object with the compiled result and the parsed AST.
Params
str
{String}options
{Object}returns
{String}
Example
Returns an array of matches captured by pattern
in string
, or null
if the pattern did not match.
Params
pattern
{String}: Glob pattern to use for matching.string
{String}: String to matchoptions
{Object}: See available options for changing how matches are performedreturns
{Boolean}: Returns an array of captures if the string matches the glob pattern, otherwisenull
.
Example
Create a regular expression from the given pattern
and options
.
Params
pattern
{String}: The pattern to convert to regex.options
{Object}returns
{RegExp}
Example
Options
Available options are based on the options from Bash (and the option names used in bash).
options.nullglob
Type: boolean
Default: undefined
When enabled, the pattern itself will be returned when no matches are found.
options.nonull
Alias for options.nullglob, included for parity with minimatch.
options.cache
Type: boolean
Default: undefined
Functions are memoized based on the given glob patterns and options. Disable memoization by setting options.cache
to false.
options.failglob
Type: boolean
Default: undefined
Throw an error is no matches are found.
Benchmarks
Last run on December 21, 2017
Differences from Bash
This library has complete parity with Bash 4.3 with only a couple of minor differences.
In some cases Bash returns true if the given string "contains" the pattern, whereas this library returns true if the string is an exact match for the pattern. You can relax this by setting
options.contains
to true.This library is more accurate than Bash and thus does not fail some of the tests that Bash 4.3 still lists as failing in their unit tests
About
Related projects
You might also be interested in these projects:
expand-brackets: Expand POSIX bracket expressions (character classes) in glob patterns. | homepage
expand-range: Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. Used… more | homepage
fill-range: Fill in a range of numbers or letters, optionally passing an increment or
step
to… more | homepagemicromatch: Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | homepage
Contributors
Commits | Contributor |
49 | |
2 | |
1 | |
1 | |
1 | |
1 |
Author
Jon Schlinkert
License
Copyright © 2017, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on December 21, 2017.
`@` isn "'t a RegEx character." ↩
Last updated