> Fast, minimal glob matcher for node.js. Similar to micromatch, minimatch and multimatch, but complete Bash 4.3 wildcard support only (no support for exglobs, posix brackets or braces)
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
This release includes fixes for windows path edge cases and other improvements for stricter adherence to bash spec.
**Fixed**
* More windows path edge cases
**Added**
* Support for bash-like quoted strings for escaping sequences of characters, such as `foo/"**"/bar` where `**` should be matched literally and not evaluated as special characters.
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](#getting-started): learn how to install and begin using nanomatch
* [Features](#features): jump to info about supported patterns, and a glob matching reference
* [API documentation](#api): jump to available options and methods
* [Unit tests](test): 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!
<details>
<summary><strong>How is this different?</strong></summary>
**Speed and accuracy**
Nanomatch uses [snapdragon](https://github.com/jonschlinkert/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](../../issues), [please let us know](../../issues/new).
**Basic globbing only**
Nanomatch supports [basic globbing only](#features), which is limited to `*`, `**`, `?` and regex-like brackets.
If you need support for the other [bash "expansion" types](#bash-expansion-libs) (in addition to the wildcard matching provided by nanomatch), consider using [micromatch](https://github.com/micromatch/micromatch) instead. _(micromatch >=3.0.0 uses the nanomatch parser and compiler for basic glob matching)_
Add nanomatch to your project using node's `require()` system:
```js
var nanomatch = require('nanomatch');
// 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](#features) to use for matching.
*`options`**{Object}**: Any [supported options](#options) may be passed
**Examples**
```js
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](#api) for available methods and [options](https://github.com/einaros/options.js).
## Documentation
### Escaping
_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:
```js
'foo/\*/bar'
```
The following pattern would match `foo/` followed by a literal `*`, followed by zero or more of any characters besides `/`, followed by `/bar`.
```js
'foo/\**/bar'
```
**Quoted strings**
Use single or double quotes to escape sequences of characters. For example, the following patterns would match `foo/**/bar` exactly:
```js
'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`:
```js
'foo/\\"*\\"/bar'
```
And the following will match `foo/'*'/bar`, `foo/'a'/bar`, `foo/'b'/bar`, or `foo/'c'/bar`:
```js
'foo/\\\'*\\\'/bar'
```
## API
### [nanomatch](index.js#L40)
The main function takes a list of strings and one or more glob patterns to use for matching.
**Params**
*`list`**{Array}**: A list of strings to match
*`patterns`**{String|Array}**: One or more glob patterns to use for matching.
*`options`**{Object}**: See available [options](#options) for changing how matches are performed
*`returns`**{Array}**: Returns an array of matches
**Example**
```js
var nm = require('nanomatch');
nm(list, patterns[, options]);
console.log(nm(['a.js', 'a.txt'], ['*.js']));
//=> [ 'a.js' ]
```
### [.match](index.js#L106)
Similar to the main function, but `pattern` must be a string.
**Params**
*`list`**{Array}**: Array of strings to match
*`pattern`**{String}**: Glob pattern to use for matching.
*`options`**{Object}**: See available [options](#options) for changing how matches are performed
*`returns`**{Array}**: Returns an array of matches
Returns true if the given `string` contains the given pattern. Similar to [.isMatch](#isMatch) but the pattern can match any part of the string.
**Params**
*`str`**{String}**: The string to match.
*`patterns`**{String|Array}**: Glob pattern to use for matching.
*`options`**{Object}**: See available [options](#options) for changing how matches are performed
*`returns`**{Boolean}**: Returns true if the patter matches any part of `str`.
**Example**
```js
var nm = require('nanomatch');
nm.contains(string, pattern[, options]);
console.log(nm.contains('aa/bb/cc', '*b'));
//=> true
console.log(nm.contains('aa/bb/cc', '*d'));
//=> false
```
### [.matchKeys](index.js#L450)
Filter the keys of the given object with the given `glob` pattern and `options`. Does not attempt to match nested keys. If you need this feature, use [glob-object](https://github.com/jonschlinkert/glob-object) instead.
**Params**
*`object`**{Object}**: The object with keys to filter.
*`patterns`**{String|Array}**: One or more glob patterns to use for matching.
*`options`**{Object}**: See available [options](#options) for changing how matches are performed
*`returns`**{Object}**: Returns an object with only keys that match the given patterns.
**Example**
```js
var nm = require('nanomatch');
nm.matchKeys(object, patterns[, options]);
var obj = { aa: 'a', ab: 'b', ac: 'c' };
console.log(nm.matchKeys(obj, '*b'));
//=> { ab: 'b' }
```
### [.matcher](index.js#L479)
Returns a memoized matcher function from the given glob `pattern` and `options`. The returned function takes a string to match as its only argument and returns true if the string is a match.
**Params**
*`pattern`**{String}**: Glob pattern
*`options`**{Object}**: See available [options](#options) for changing how matches are performed.
*`returns`**{Function}**: Returns a matcher function.
Allow glob patterns without slashes to match a file path based on its basename. Same behavior as [minimatch](https://github.com/isaacs/minimatch) option `matchBase`.
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**
```js
var files = ['abc', 'ajz'];
console.log(nm(files, '[a-c]*'));
//=> ['abc', 'ajz']
console.log(nm(files, '[a-c]*', {bash: false}));
```
</details>
<details>
<summary><strong>cache</strong></summary>
### options.cache
Disable regex and function memoization.
Type: `Boolean`
Default: `undefined`
</details>
<details>
<summary><strong>dot</strong></summary>
### options.dot
Match dotfiles. Same behavior as [minimatch](https://github.com/isaacs/minimatch) option `dot`.
Type: `Boolean`
Default: `false`
</details>
<details>
<summary><strong>failglob</strong></summary>
### options.failglob
Similar to the `--failglob` behavior in Bash, throws an error when no matches are found.
Type: `Boolean`
Default: `undefined`
</details>
<details>
<summary><strong>ignore</strong></summary>
### options.ignore
String or array of glob patterns to match files to ignore.
Type: `String|Array`
Default: `undefined`
</details>
<details>
<summary><strong>matchBase</strong></summary>
### options.matchBase
Alias for [options.basename](#options-basename).
</details>
<details>
<summary><strong>nocase</strong></summary>
### options.nocase
Use a case-insensitive regex for matching files. Same behavior as [minimatch](https://github.com/isaacs/minimatch).
Type: `Boolean`
Default: `undefined`
</details>
<details>
<summary><strong>nodupes</strong></summary>
### options.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:
Disallow negation (`!`) patterns, and treat leading `!` as a literal character to match.
Type: `Boolean`
Default: `undefined`
</details>
<details>
<summary><strong>nonull</strong></summary>
### options.nonull
Alias for [options.nullglob](#options-nullglob).
</details>
<details>
<summary><strong>nullglob</strong></summary>
### options.nullglob
If `true`, when no matches are found the actual (arrayified) glob pattern is returned instead of an empty array. Same behavior as [minimatch](https://github.com/isaacs/minimatch) option `nonull`.
Type: `Boolean`
Default: `undefined`
</details>
<details>
<summary><strong>snapdragon</strong></summary>
### options.snapdragon
Pass your own instance of [snapdragon](https://github.com/jonschlinkert/snapdragon) to customize parsers or compilers.
Type: `Object`
Default: `undefined`
</details>
<details>
<summary><strong>snapdragon</strong></summary>
### options.sourcemap
Generate a source map by enabling the `sourcemap` option with the `.parse`, `.compile`, or `.create` methods.
**Examples**
```js
var nm = require('nanomatch');
var res = nm.create('abc/*.js', {sourcemap: true});
Convert path separators on returned files to posix/unix-style forward slashes.
Type: `Boolean`
Default: `true`
**Example**
```js
nm.match(['a\\b\\c'], 'a/**');
//=> ['a/b/c']
nm.match(['a\\b\\c'], {unixify: false});
//=> ['a\\b\\c']
```
</details>
## Features
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 | when it's not the only thing in a path segment, per Bash specifications) |
| `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:
Nanomatch is part of a suite of libraries aimed at bringing the power and expressiveness of [Bash's](https://www.gnu.org/software/bash/) matching and expansion capabilities to JavaScript, _and - as you can see by the [benchmarks](#benchmarks) - without sacrificing speed_.
| `nanomatch` (you are here) | Wildcards | `*` | [Filename expansion](https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html#Filename-Expansion), also referred to as globbing and pathname expansion, allows the use of [wildcards](#features) for matching. |
| [expand-tilde](https://github.com/jonschlinkert/expand-tilde) | Tildes | `~` | [Tilde expansion](https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html#Tilde-Expansion) converts the leading tilde in a file path to the user home directory. |
| [expand-brackets](https://github.com/jonschlinkert/expand-brackets) | Brackets | `[[:alpha:]]` | [POSIX character classes](https://www.gnu.org/software/grep/manual/html_node/Character-Classes-and-Bracket-Expressions.html) (also referred to as POSIX brackets, or POSIX character classes) |
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
To generate the readme, run the following command:
* [extglob](https://www.npmjs.com/package/extglob): Extended glob support for JavaScript. Adds (almost) the expressive power of regular expressions to glob… [more](https://github.com/micromatch/extglob) | [homepage](https://github.com/micromatch/extglob "Extended glob support for JavaScript. Adds (almost) the expressive power of regular expressions to glob patterns.")
* [is-extglob](https://www.npmjs.com/package/is-extglob): Returns true if a string has an extglob. | [homepage](https://github.com/jonschlinkert/is-extglob "Returns true if a string has an extglob.")
* [is-glob](https://www.npmjs.com/package/is-glob): Returns `true` if the given string looks like a glob pattern or an extglob pattern… [more](https://github.com/jonschlinkert/is-glob) | [homepage](https://github.com/jonschlinkert/is-glob "Returns `true` if the given string looks like a glob pattern or an extglob pattern. This makes it easy to create code that only uses external modules like node-glob when necessary, resulting in much faster code execution and initialization time, and a bet")
* [micromatch](https://www.npmjs.com/package/micromatch): Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | [homepage](https://github.com/micromatch/micromatch "Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.")