Release history
{%= changelog("changelog.md") %}
Nanomatch is a fast and accurate glob matcher with full support for standard Bash glob features, including the following "metacharacters": *
, **
, ?
and [...]
.
Learn more
- Getting started: learn how to install and begin using nanomatch
- Features: jump to info about supported patterns, and a glob matching reference
- API documentation: jump to available options and methods
- Unit tests: visit unit tests. there is no better way to learn a code library than spending time the unit tests. Nanomatch has 36,000 unit tests - go become a glob matching ninja!
How is this different?
Speed and accuracy
Nanomatch uses [snapdragon][] for parsing and compiling globs, which results in:
- Granular control over the entire conversion process in a way that is easy to understand, reason about, and customize.
- Faster matching, from a combination of optimized glob patterns and (optional) caching.
- Much greater accuracy than minimatch. In fact, nanomatch passes all of the spec tests from bash, including some that bash still fails. However, since there is no real specification for globs, if you encounter a pattern that yields unexpected match results after researching previous issues, please let us know.
Basic globbing only
Nanomatch supports basic globbing only, which is limited to *
, **
, ?
and regex-like brackets.
If you need support for the other bash "expansion" types (in addition to the wildcard matching provided by nanomatch), consider using [micromatch][] instead. (micromatch >=3.0.0 uses the nanomatch parser and compiler for basic glob matching)
Install with yarn
$ yarn add nanomatch
Install with npm
$ npm install nanomatch
Add nanomatch to your project using node's require()
system:
var nanomatch = require('{%= name %}');
// the main export is a function that takes an array of strings to match
// and a string or array of patterns to use for matching
nanomatch(list, patterns[, options]);
Params
list
{String|Array}: List of strings to perform matches against. This is often a list of file paths.patterns
{String|Array}: One or more glob paterns to use for matching.options
{Object}: Any supported options may be passed
Examples
var nm = require('nanomatch');
console.log(nm(['a', 'b/b', 'c/c/c'], '*'));
//=> ['a']
console.log(nm(['a', 'b/b', 'c/c/c'], '*/*'));
//=> ['b/b']
console.log(nm(['a', 'b/b', 'c/c/c'], '**'));
//=> ['a', 'b/b', 'c/c/c']
See the API documentation for available methods and [options][].
Backslashes and quotes can be used to escape characters, forcing nanomatch to regard those characters as a literal characters.
Backslashes
Use backslashes to escape single characters. For example, the following pattern would match foo/*/bar
exactly:
'foo/\*/bar'
The following pattern would match foo/
followed by a literal *
, followed by zero or more of any characters besides /
, followed by /bar
.
'foo/\**/bar'
Quoted strings
Use single or double quotes to escape sequences of characters. For example, the following patterns would match foo/**/bar
exactly:
'foo/"**"/bar'
'foo/\'**\'/bar'
"foo/'**'/bar"
Matching literal quotes
If you need to match quotes literally, you can escape them as well. For example, the following will match foo/"*"/bar
, foo/"a"/bar
, foo/"b"/bar
, or foo/"c"/bar
:
'foo/\\"*\\"/bar'
And the following will match foo/'*'/bar
, foo/'a'/bar
, foo/'b'/bar
, or foo/'c'/bar
:
'foo/\\\'*\\\'/bar'
{%= apidocs("index.js") %}
basename
Allow glob patterns without slashes to match a file path based on its basename. Same behavior as [minimatch][] option matchBase
.
Type: boolean
Default: false
Example
nm(['a/b.js', 'a/c.md'], '*.js');
//=> []
nm(['a/b.js', 'a/c.md'], '*.js', {matchBase: true});
//=> ['a/b.js']
bash
Enabled by default, this option enforces bash-like behavior with stars immediately following a bracket expression. Bash bracket expressions are similar to regex character classes, but unlike regex, a star following a bracket expression does not repeat the bracketed characters. Instead, the star is treated the same as an other star.
Type: boolean
Default: true
Example
var files = ['abc', 'ajz'];
console.log(nm(files, '[a-c]*'));
//=> ['abc', 'ajz']
console.log(nm(files, '[a-c]*', {bash: false}));
dot
Match dotfiles. Same behavior as [minimatch][] option dot
.
Type: boolean
Default: false
failglob
Similar to the --failglob
behavior in Bash, throws an error when no matches are found.
Type: boolean
Default: undefined
ignore
String or array of glob patterns to match files to ignore.
Type: String|Array
Default: undefined
nocase
Use a case-insensitive regex for matching files. Same behavior as [minimatch][].
Type: boolean
Default: undefined
nodupes
Remove duplicate elements from the result array.
Type: boolean
Default: true
(enabled by default)
Example
Example of using the unescape
and nodupes
options together:
nm.match(['a/b/c', 'a/b/c'], '**');
//=> ['abc']
nm.match(['a/b/c', 'a/b/c'], '**', {nodupes: false});
//=> ['a/b/c', 'a/b/c']
noglobstar
Disable matching with globstars (**
).
Type: boolean
Default: undefined
nm(['a/b', 'a/b/c', 'a/b/c/d'], 'a/**');
//=> ['a/b', 'a/b/c', 'a/b/c/d']
nm(['a/b', 'a/b/c', 'a/b/c/d'], 'a/**', {noglobstar: true});
//=> ['a/b']
nonegate
Disallow negation (!
) patterns, and treat leading !
as a literal character to match.
Type: boolean
Default: undefined
nullglob
If true
, when no matches are found the actual (arrayified) glob pattern is returned instead of an empty array. Same behavior as [minimatch][] option nonull
.
Type: boolean
Default: undefined
slash
Customize the slash character(s) to use for matching.
Type: string|function
Default: [/\\]
(forward slash and backslash)
star
Customize the star character(s) to use for matching. It's not recommended that you modify this unless you have advanced knowledge of the compiler and matching rules.
Type: string|function
Default: [^/\\]*?
snapdragon
Pass your own instance of [snapdragon][] to customize parsers or compilers.
Type: object
Default: undefined
sourcemap
Generate a source map by enabling the sourcemap
option with the .parse
, .compile
, or .create
methods.
Examples
var nm = require('nanomatch');
var res = nm.create('abc/*.js', {sourcemap: true});
console.log(res.map);
// { version: 3,
// sources: [ 'string' ],
// names: [],
// mappings: 'AAAA,GAAG,EAAC,iBAAC,EAAC,EAAE',
// sourcesContent: [ 'abc/*.js' ] }
var ast = nm.parse('abc/**/*.js');
var res = nm.compile(ast, {sourcemap: true});
console.log(res.map);
// { version: 3,
// sources: [ 'string' ],
// names: [],
// mappings: 'AAAA,GAAG,EAAC,2BAAE,EAAC,iBAAC,EAAC,EAAE',
// sourcesContent: [ 'abc/**/*.js' ] }
unescape
Remove backslashes from returned matches.
Type: boolean
Default: undefined
Example
In this example we want to match a literal *
:
nm.match(['abc', 'a\\*c'], 'a\\*c');
//=> ['a\\*c']
nm.match(['abc', 'a\\*c'], 'a\\*c', {unescape: true});
//=> ['a*c']
unixify
Convert path separators on returned files to posix/unix-style forward slashes.
Type: boolean
Default: true
Example
nm.match(['a\\b\\c'], 'a/**');
//=> ['a/b/c']
nm.match(['a\\b\\c'], {unixify: false});
//=> ['a\\b\\c']
Nanomatch has full support for standard Bash glob features, including the following "metacharacters": *
, **
, ?
and [...]
.
Here are some examples of how they work:
Pattern | Description |
---|---|
* |
Matches any string except for / , leading . , or /. inside a path |
** |
Matches any string including / , but not a leading . or /. inside a path. More than two stars (e.g. *** is treated the same as one star, and ** loses its special meaning |
foo* |
Matches any string beginning with foo |
*bar* |
Matches any string containing bar (beginning, middle or end) |
*.min.js |
Matches any string ending with .min.js |
[abc]*.js |
Matches any string beginning with a , b , or c and ending with .js |
abc? |
Matches abcd or abcz but not abcde |
The exceptions noted for *
apply to all patterns that contain a *
.
Not supported
The following extended-globbing features are not supported:
- brace expansion (e.g.
{a,b,c}
) - extglobs (e.g.
@(a|!(c|d))
) - POSIX brackets (e.g.
[[:alpha:][:digit:]]
)
If you need any of these features consider using [micromatch][] instead.
Nanomatch is part of a suite of libraries aimed at bringing the power and expressiveness of Bash's matching and expansion capabilities to JavaScript, and - as you can see by the benchmarks - without sacrificing speed.
Related library | Matching Type | Example | Description |
---|---|---|---|
nanomatch (you are here) |
Wildcards | * |
Filename expansion, also referred to as globbing and pathname expansion, allows the use of wildcards for matching. |
[expand-tilde][] | Tildes | ~ |
Tilde expansion converts the leading tilde in a file path to the user home directory. |
braces | Braces | {a,b,c} |
Brace expansion |
[expand-brackets][] | Brackets | [[:alpha:]] |
POSIX character classes (also referred to as POSIX brackets, or POSIX character classes) |
extglob | Parens | !(a|b) |
Extglobs |
[micromatch][] | All | all | Micromatch is built on top of the other libraries. |
There are many resources available on the web if you want to dive deeper into how these features work in Bash.
Install dev dependencies:
npm i -d && node benchmark
{%= include("benchmark/stats.md") %}