Picomatch
Blazing fast and accurate glob matcher written in JavaScript. No dependencies and full support for standard and extended Bash glob features, including braces, extglobs, POSIX brackets, and regular expressions.
Why picomatch?
Lightweight - No dependencies
Minimal - Tiny API surface. Main export is a function that takes a glob pattern and returns a matcher function.
Fast - Loads in about 2ms (that's several times faster than a single frame of a HD movie at 60fps)
Performant - Use the returned matcher function to speed up repeat matching (like when watching files)
Accurate matching - Using wildcards (
*
and?
), globstars (**
) for nested directories, advanced globbing with extglobs, braces, and POSIX brackets, and support for escaping special characters with\
or quotes.Well tested - Thousands of unit tests
See the library comparison to other libraries.
Table of Contents
Install
Install with npm:
Usage
The main export is a function that takes a glob pattern and an options object and returns a function for matching strings.
API
Creates a matcher function from one or more glob patterns. The returned function takes a string to match as its first argument, and returns true if the string is a match. The returned matcher function also takes a boolean as the second argument that, when true, returns an object with additional information.
Params
globs
{String|Array}: One or more glob patterns.options
{Object=}returns
{Function=}: Returns a matcher function.
Example
Test input
with the given regex
. This is used by the main picomatch()
function to test the input string.
Params
input
{String}: String to test.regex
{RegExp}returns
{Object}: Returns an object with matching info.
Example
Match the basename of a filepath.
Params
input
{String}: String to test.glob
{RegExp|String}: Glob pattern or regex created by .makeRe.returns
{Boolean}
Example
Returns true if any of the given glob patterns
match the specified string
.
Params
{String|Array}: str The string to test.
{String|Array}: patterns One or more glob patterns to use for matching.
{Object}: See available options.
returns
{Boolean}: Returns true if any patterns matchstr
Example
Parse a glob pattern to create the source string for a regular expression.
Params
pattern
{String}options
{Object}returns
{Object}: Returns an object with useful properties and output to be used as a regex source string.
Example
Scan a glob pattern to separate the pattern into segments.
Params
input
{String}: Glob pattern to scan.options
{Object}returns
{Object}: Returns an object with
Example
Compile a regular expression from the state
object returned by the parse() method.
Params
state
{Object}options
{Object}returnOutput
{Boolean}: Intended for implementors, this argument allows you to return the raw output from the parser.returnState
{Boolean}: Adds the state to astate
property on the returned regex. Useful for implementors and debugging.returns
{RegExp}
Create a regular expression from a parsed glob pattern.
Params
state
{String}: The object returned from the.parse
method.options
{Object}returnOutput
{Boolean}: Implementors may use this argument to return the compiled output, instead of a regular expression. This is not exposed on the options to prevent end-users from mutating the result.returnState
{Boolean}: Implementors may use this argument to return the state from the parsed glob with the returned regular expression.returns
{RegExp}: Returns a regex created from the given pattern.
Example
Create a regular expression from the given regex source string.
Params
source
{String}: Regular expression source string.options
{Object}returns
{RegExp}
Example
Options
Picomatch options
The following options may be used with the main picomatch()
function or any of the methods on the picomatch API.
Option | Type | Default value | Description |
|
|
| If set, then patterns without slashes will be matched against the basename of the path if it contains slashes. For example, |
|
|
| Follow bash matching rules more strictly - disallows backslashes as escape characters, and treats single stars as globstars ( |
|
|
| Return regex matches in supporting methods. |
|
|
| Allows glob to match any part of the given string(s). |
|
|
| Current working directory. Used by |
|
|
| Debug regular expressions when an error is thrown. |
|
|
| Enable dotfile matching. By default, dotfiles are ignored unless a |
|
|
| Custom function for expanding ranges in brace patterns, such as |
|
|
| Throws an error if no matches are found. Based on the bash option of the same name. |
|
|
| To speed up processing, full parsing is skipped for a handful common glob patterns. Disable this behavior by setting this option to |
|
|
| Regex flags to use in the generated regex. If defined, the |
|
| Custom function for formatting the returned string. This is useful for removing leading slashes, converting Windows paths to Posix paths, etc. | |
|
|
| One or more glob patterns for excluding strings that should not be matched from the result. |
|
|
| Retain quotes in the generated regex, since quotes may also be used as an alternative to backslashes. |
|
|
| When |
|
|
| Support regex positive and negative lookbehinds. Note that you must be using Node 8.1.10 or higher to enable regex lookbehinds. |
|
|
| Alias for |
|
|
| Limit the max length of the input string. An error is thrown if the input string is longer than this value. |
|
|
| Disable brace matching, so that |
|
|
| Disable matching with regex brackets. |
|
|
| Make matching case-insensitive. Equivalent to the regex |
|
|
| Deprecated, use |
|
|
| Alias for |
|
|
| Disable support for matching with extglobs (like |
|
|
| Disable support for matching nested directories with globstars ( |
|
|
| Disable support for negating with leading |
|
|
| Disable support for regex quantifiers (like |
|
| Function to be called on ignored items. | |
|
| Function to be called on matched items. | |
|
| Function to be called on all items, regardless of whether or not they are matched or ignored. | |
|
|
| Support POSIX character classes ("posix brackets"). |
|
|
| Convert all slashes in file paths to forward slashes. This does not convert slashes in the glob pattern itself |
|
|
| String to prepend to the generated regex used for matching. |
|
|
| Use regular expression rules for |
|
|
| Throw an error if brackets, braces, or parens are imbalanced. |
|
|
| When true, picomatch won't match trailing slashes with single stars. |
|
|
| Remove backslashes preceding escaped characters in the glob pattern. By default, backslashes are retained. |
|
|
| Alias for |
Scan Options
In addition to the main picomatch options, the following options may also be used with the .scan method.
Option | Type | Default value | Description |
|
|
| When |
|
|
| When |
Example
Options Examples
options.expandRange
Type: function
Default: undefined
Custom function for expanding ranges in brace patterns. The fill-range library is ideal for this purpose, or you can use custom code to do whatever you need.
Example
The following example shows how to create a glob that matches a folder
options.format
Type: function
Default: undefined
Custom function for formatting strings before they're matched.
Example
options.onMatch
options.onIgnore
options.onResult
Globbing features
Basic globbing (Wildcard matching)
Advanced globbing (extglobs, posix brackets, brace matching)
Basic globbing
Character | Description |
| Matches any character zero or more times, excluding path separators. Does not match path separators or hidden files or directories ("dotfiles"), unless explicitly enabled by setting the |
| Matches any character zero or more times, including path separators. Note that |
| Matches any character excluding path separators one time. Does not match path separators or leading dots. |
| Matches any characters inside the brackets. For example, |
Matching behavior vs. Bash
Picomatch's matching features and expected results in unit tests are based on Bash's unit tests and the Bash 4.3 specification, with the following exceptions:
Bash will match
foo/bar/baz
with*
. Picomatch only matches nested directories with**
.Bash greedily matches with negated extglobs. For example, Bash 4.3 says that
!(foo)*
should matchfoo
andfoobar
, since the trailing*
bracktracks to match the preceding pattern. This is very memory-inefficient, and IMHO, also incorrect. Picomatch would returnfalse
for bothfoo
andfoobar
.
Advanced globbing
Extglobs
Pattern | Description |
| Match only one consecutive occurrence of |
| Match zero or more consecutive occurrences of |
| Match one or more consecutive occurrences of |
| Match zero or one consecutive occurrences of |
| Match anything but |
Examples
POSIX brackets
POSIX classes are disabled by default. Enable this feature by setting the posix
option to true.
Enable POSIX bracket support
Supported POSIX classes
The following named POSIX bracket expressions are supported:
[:alnum:]
- Alphanumeric characters, equ[a-zA-Z0-9]
[:alpha:]
- Alphabetical characters, equivalent to[a-zA-Z]
.[:ascii:]
- ASCII characters, equivalent to[\\x00-\\x7F]
.[:blank:]
- Space and tab characters, equivalent to[ \\t]
.[:cntrl:]
- Control characters, equivalent to[\\x00-\\x1F\\x7F]
.[:digit:]
- Numerical digits, equivalent to[0-9]
.[:graph:]
- Graph characters, equivalent to[\\x21-\\x7E]
.[:lower:]
- Lowercase letters, equivalent to[a-z]
.[:print:]
- Print characters, equivalent to[\\x20-\\x7E ]
.[:punct:]
- Punctuation and symbols, equivalent to[\\-!"#$%&\'()\\*+,./:;<=>?@[\\]^_
{|}~]`.[:space:]
- Extended space characters, equivalent to[ \\t\\r\\n\\v\\f]
.[:upper:]
- Uppercase letters, equivalent to[A-Z]
.[:word:]
- Word characters (letters, numbers and underscores), equivalent to[A-Za-z0-9_]
.[:xdigit:]
- Hexadecimal digits, equivalent to[A-Fa-f0-9]
.
See the Bash Reference Manual for more information.
Braces
Picomatch does not do brace expansion. For brace expansion and advanced matching with braces, use micromatch instead. Picomatch has very basic support for braces.
Matching special characters as literals
If you wish to match the following special characters in a filepath, and you want to use these characters in your glob pattern, they must be escaped with backslashes or quotes:
Special Characters
Some characters that are used for matching in regular expressions are also regarded as valid file path characters on some platforms.
To match any of the following characters as literals: `$^*+?()[]
Examples:
Library Comparisons
The following table shows which features are supported by minimatch, micromatch, picomatch, nanomatch, extglob, braces, and expand-brackets.
Feature |
|
|
|
|
|
|
|
Wildcard matching ( | ✔ | ✔ | ✔ | ✔ | - | - | - |
Advancing globbing | ✔ | ✔ | ✔ | - | - | - | - |
Brace matching | ✔ | ✔ | ✔ | - | - | ✔ | - |
Brace expansion | ✔ | ✔ | - | - | - | ✔ | - |
Extglobs | partial | ✔ | ✔ | - | ✔ | - | - |
Posix brackets | - | ✔ | ✔ | - | - | - | ✔ |
Regular expression syntax | - | ✔ | ✔ | ✔ | ✔ | - | ✔ |
File system operations | - | - | - | - | - | - | - |
Benchmarks
Performance comparison of picomatch and minimatch.
Philosophies
The goal of this library is to be blazing fast, without compromising on accuracy.
Accuracy
The number one of goal of this library is accuracy. However, it's not unusual for different glob implementations to have different rules for matching behavior, even with simple wildcard matching. It gets increasingly more complicated when combinations of different features are combined, like when extglobs are combined with globstars, braces, slashes, and so on: !(**/{a,b,*/c})
.
Thus, given that there is no canonical glob specification to use as a single source of truth when differences of opinion arise regarding behavior, sometimes we have to implement our best judgement and rely on feedback from users to make improvements.
Performance
Although this library performs well in benchmarks, and in most cases it's faster than other popular libraries we benchmarked against, we will always choose accuracy over performance. It's not helpful to anyone if our library is faster at returning the wrong answer.
About
Author
Jon Schlinkert
License
Copyright © 2017-present, Jon Schlinkert. Released under the MIT License.
Last updated