| [9af201e] | 1 | # minimatch
|
|---|
| 2 |
|
|---|
| 3 | A minimal matching utility.
|
|---|
| 4 |
|
|---|
| 5 | [](http://travis-ci.org/isaacs/minimatch)
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 | This is the matching library used internally by npm.
|
|---|
| 9 |
|
|---|
| 10 | It works by converting glob expressions into JavaScript `RegExp`
|
|---|
| 11 | objects.
|
|---|
| 12 |
|
|---|
| 13 | ## Important Security Consideration!
|
|---|
| 14 |
|
|---|
| 15 | > [!WARNING]
|
|---|
| 16 | > This library uses JavaScript regular expressions. Please read
|
|---|
| 17 | > the following warning carefully, and be thoughtful about what
|
|---|
| 18 | > you provide to this library in production systems.
|
|---|
| 19 |
|
|---|
| 20 | _Any_ library in JavaScript that deals with matching string
|
|---|
| 21 | patterns using regular expressions will be subject to
|
|---|
| 22 | [ReDoS](https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS)
|
|---|
| 23 | if the pattern is generated using untrusted input.
|
|---|
| 24 |
|
|---|
| 25 | Efforts have been made to mitigate risk as much as is feasible in
|
|---|
| 26 | such a library, providing maximum recursion depths and so forth,
|
|---|
| 27 | but these measures can only ultimately protect against accidents,
|
|---|
| 28 | not malice. A dedicated attacker can _always_ find patterns that
|
|---|
| 29 | cannot be defended against by a bash-compatible glob pattern
|
|---|
| 30 | matching system that uses JavaScript regular expressions.
|
|---|
| 31 |
|
|---|
| 32 | To be extremely clear:
|
|---|
| 33 |
|
|---|
| 34 | > [!WARNING]
|
|---|
| 35 | > **If you create a system where you take user input, and use
|
|---|
| 36 | > that input as the source of a Regular Expression pattern, in
|
|---|
| 37 | > this or any extant glob matcher in JavaScript, you will be
|
|---|
| 38 | > pwned.**
|
|---|
| 39 |
|
|---|
| 40 | A future version of this library _may_ use a different matching
|
|---|
| 41 | algorithm which does not exhibit backtracking problems. If and
|
|---|
| 42 | when that happens, it will likely be a sweeping change, and those
|
|---|
| 43 | improvements will **not** be backported to legacy versions.
|
|---|
| 44 |
|
|---|
| 45 | In the near term, it is not reasonable to continue to play
|
|---|
| 46 | whack-a-mole with security advisories, and so any future ReDoS
|
|---|
| 47 | reports will be considered "working as intended", and resolved
|
|---|
| 48 | entirely by this warning.
|
|---|
| 49 |
|
|---|
| 50 | ## Usage
|
|---|
| 51 |
|
|---|
| 52 | ```javascript
|
|---|
| 53 | var minimatch = require("minimatch")
|
|---|
| 54 |
|
|---|
| 55 | minimatch("bar.foo", "*.foo") // true!
|
|---|
| 56 | minimatch("bar.foo", "*.bar") // false!
|
|---|
| 57 | minimatch("bar.foo", "*.+(bar|foo)", { debug: true }) // true, and noisy!
|
|---|
| 58 | ```
|
|---|
| 59 |
|
|---|
| 60 | ## Features
|
|---|
| 61 |
|
|---|
| 62 | Supports these glob features:
|
|---|
| 63 |
|
|---|
| 64 | * Brace Expansion
|
|---|
| 65 | * Extended glob matching
|
|---|
| 66 | * "Globstar" `**` matching
|
|---|
| 67 |
|
|---|
| 68 | See:
|
|---|
| 69 |
|
|---|
| 70 | * `man sh`
|
|---|
| 71 | * `man bash`
|
|---|
| 72 | * `man 3 fnmatch`
|
|---|
| 73 | * `man 5 gitignore`
|
|---|
| 74 |
|
|---|
| 75 | ## Minimatch Class
|
|---|
| 76 |
|
|---|
| 77 | Create a minimatch object by instantiating the `minimatch.Minimatch` class.
|
|---|
| 78 |
|
|---|
| 79 | ```javascript
|
|---|
| 80 | var Minimatch = require("minimatch").Minimatch
|
|---|
| 81 | var mm = new Minimatch(pattern, options)
|
|---|
| 82 | ```
|
|---|
| 83 |
|
|---|
| 84 | ### Properties
|
|---|
| 85 |
|
|---|
| 86 | * `pattern` The original pattern the minimatch object represents.
|
|---|
| 87 | * `options` The options supplied to the constructor.
|
|---|
| 88 | * `set` A 2-dimensional array of regexp or string expressions.
|
|---|
| 89 | Each row in the
|
|---|
| 90 | array corresponds to a brace-expanded pattern. Each item in the row
|
|---|
| 91 | corresponds to a single path-part. For example, the pattern
|
|---|
| 92 | `{a,b/c}/d` would expand to a set of patterns like:
|
|---|
| 93 |
|
|---|
| 94 | [ [ a, d ]
|
|---|
| 95 | , [ b, c, d ] ]
|
|---|
| 96 |
|
|---|
| 97 | If a portion of the pattern doesn't have any "magic" in it
|
|---|
| 98 | (that is, it's something like `"foo"` rather than `fo*o?`), then it
|
|---|
| 99 | will be left as a string rather than converted to a regular
|
|---|
| 100 | expression.
|
|---|
| 101 |
|
|---|
| 102 | * `regexp` Created by the `makeRe` method. A single regular expression
|
|---|
| 103 | expressing the entire pattern. This is useful in cases where you wish
|
|---|
| 104 | to use the pattern somewhat like `fnmatch(3)` with `FNM_PATH` enabled.
|
|---|
| 105 | * `negate` True if the pattern is negated.
|
|---|
| 106 | * `comment` True if the pattern is a comment.
|
|---|
| 107 | * `empty` True if the pattern is `""`.
|
|---|
| 108 |
|
|---|
| 109 | ### Methods
|
|---|
| 110 |
|
|---|
| 111 | * `makeRe` Generate the `regexp` member if necessary, and return it.
|
|---|
| 112 | Will return `false` if the pattern is invalid.
|
|---|
| 113 | * `match(fname)` Return true if the filename matches the pattern, or
|
|---|
| 114 | false otherwise.
|
|---|
| 115 | * `matchOne(fileArray, patternArray, partial)` Take a `/`-split
|
|---|
| 116 | filename, and match it against a single row in the `regExpSet`. This
|
|---|
| 117 | method is mainly for internal use, but is exposed so that it can be
|
|---|
| 118 | used by a glob-walker that needs to avoid excessive filesystem calls.
|
|---|
| 119 |
|
|---|
| 120 | All other methods are internal, and will be called as necessary.
|
|---|
| 121 |
|
|---|
| 122 | ### minimatch(path, pattern, options)
|
|---|
| 123 |
|
|---|
| 124 | Main export. Tests a path against the pattern using the options.
|
|---|
| 125 |
|
|---|
| 126 | ```javascript
|
|---|
| 127 | var isJS = minimatch(file, "*.js", { matchBase: true })
|
|---|
| 128 | ```
|
|---|
| 129 |
|
|---|
| 130 | ### minimatch.filter(pattern, options)
|
|---|
| 131 |
|
|---|
| 132 | Returns a function that tests its
|
|---|
| 133 | supplied argument, suitable for use with `Array.filter`. Example:
|
|---|
| 134 |
|
|---|
| 135 | ```javascript
|
|---|
| 136 | var javascripts = fileList.filter(minimatch.filter("*.js", {matchBase: true}))
|
|---|
| 137 | ```
|
|---|
| 138 |
|
|---|
| 139 | ### minimatch.match(list, pattern, options)
|
|---|
| 140 |
|
|---|
| 141 | Match against the list of
|
|---|
| 142 | files, in the style of fnmatch or glob. If nothing is matched, and
|
|---|
| 143 | options.nonull is set, then return a list containing the pattern itself.
|
|---|
| 144 |
|
|---|
| 145 | ```javascript
|
|---|
| 146 | var javascripts = minimatch.match(fileList, "*.js", {matchBase: true}))
|
|---|
| 147 | ```
|
|---|
| 148 |
|
|---|
| 149 | ### minimatch.makeRe(pattern, options)
|
|---|
| 150 |
|
|---|
| 151 | Make a regular expression object from the pattern.
|
|---|
| 152 |
|
|---|
| 153 | ## Options
|
|---|
| 154 |
|
|---|
| 155 | All options are `false` by default.
|
|---|
| 156 |
|
|---|
| 157 | ### debug
|
|---|
| 158 |
|
|---|
| 159 | Dump a ton of stuff to stderr.
|
|---|
| 160 |
|
|---|
| 161 | ### nobrace
|
|---|
| 162 |
|
|---|
| 163 | Do not expand `{a,b}` and `{1..3}` brace sets.
|
|---|
| 164 |
|
|---|
| 165 | ### noglobstar
|
|---|
| 166 |
|
|---|
| 167 | Disable `**` matching against multiple folder names.
|
|---|
| 168 |
|
|---|
| 169 | ### dot
|
|---|
| 170 |
|
|---|
| 171 | Allow patterns to match filenames starting with a period, even if
|
|---|
| 172 | the pattern does not explicitly have a period in that spot.
|
|---|
| 173 |
|
|---|
| 174 | Note that by default, `a/**/b` will **not** match `a/.d/b`, unless `dot`
|
|---|
| 175 | is set.
|
|---|
| 176 |
|
|---|
| 177 | ### noext
|
|---|
| 178 |
|
|---|
| 179 | Disable "extglob" style patterns like `+(a|b)`.
|
|---|
| 180 |
|
|---|
| 181 | ### nocase
|
|---|
| 182 |
|
|---|
| 183 | Perform a case-insensitive match.
|
|---|
| 184 |
|
|---|
| 185 | ### nonull
|
|---|
| 186 |
|
|---|
| 187 | When a match is not found by `minimatch.match`, return a list containing
|
|---|
| 188 | the pattern itself if this option is set. When not set, an empty list
|
|---|
| 189 | is returned if there are no matches.
|
|---|
| 190 |
|
|---|
| 191 | ### matchBase
|
|---|
| 192 |
|
|---|
| 193 | If set, then patterns without slashes will be matched
|
|---|
| 194 | against the basename of the path if it contains slashes. For example,
|
|---|
| 195 | `a?b` would match the path `/xyz/123/acb`, but not `/xyz/acb/123`.
|
|---|
| 196 |
|
|---|
| 197 | ### nocomment
|
|---|
| 198 |
|
|---|
| 199 | Suppress the behavior of treating `#` at the start of a pattern as a
|
|---|
| 200 | comment.
|
|---|
| 201 |
|
|---|
| 202 | ### nonegate
|
|---|
| 203 |
|
|---|
| 204 | Suppress the behavior of treating a leading `!` character as negation.
|
|---|
| 205 |
|
|---|
| 206 | ### flipNegate
|
|---|
| 207 |
|
|---|
| 208 | Returns from negate expressions the same as if they were not negated.
|
|---|
| 209 | (Ie, true on a hit, false on a miss.)
|
|---|
| 210 |
|
|---|
| 211 | ### partial
|
|---|
| 212 |
|
|---|
| 213 | Compare a partial path to a pattern. As long as the parts of the path that
|
|---|
| 214 | are present are not contradicted by the pattern, it will be treated as a
|
|---|
| 215 | match. This is useful in applications where you're walking through a
|
|---|
| 216 | folder structure, and don't yet have the full path, but want to ensure that
|
|---|
| 217 | you do not walk down paths that can never be a match.
|
|---|
| 218 |
|
|---|
| 219 | For example,
|
|---|
| 220 |
|
|---|
| 221 | ```js
|
|---|
| 222 | minimatch('/a/b', '/a/*/c/d', { partial: true }) // true, might be /a/b/c/d
|
|---|
| 223 | minimatch('/a/b', '/**/d', { partial: true }) // true, might be /a/b/.../d
|
|---|
| 224 | minimatch('/x/y/z', '/a/**/z', { partial: true }) // false, because x !== a
|
|---|
| 225 | ```
|
|---|
| 226 |
|
|---|
| 227 | ### allowWindowsEscape
|
|---|
| 228 |
|
|---|
| 229 | Windows path separator `\` is by default converted to `/`, which
|
|---|
| 230 | prohibits the usage of `\` as a escape character. This flag skips that
|
|---|
| 231 | behavior and allows using the escape character.
|
|---|
| 232 |
|
|---|
| 233 | ## Comparisons to other fnmatch/glob implementations
|
|---|
| 234 |
|
|---|
| 235 | While strict compliance with the existing standards is a worthwhile
|
|---|
| 236 | goal, some discrepancies exist between minimatch and other
|
|---|
| 237 | implementations, and are intentional.
|
|---|
| 238 |
|
|---|
| 239 | If the pattern starts with a `!` character, then it is negated. Set the
|
|---|
| 240 | `nonegate` flag to suppress this behavior, and treat leading `!`
|
|---|
| 241 | characters normally. This is perhaps relevant if you wish to start the
|
|---|
| 242 | pattern with a negative extglob pattern like `!(a|B)`. Multiple `!`
|
|---|
| 243 | characters at the start of a pattern will negate the pattern multiple
|
|---|
| 244 | times.
|
|---|
| 245 |
|
|---|
| 246 | If a pattern starts with `#`, then it is treated as a comment, and
|
|---|
| 247 | will not match anything. Use `\#` to match a literal `#` at the
|
|---|
| 248 | start of a line, or set the `nocomment` flag to suppress this behavior.
|
|---|
| 249 |
|
|---|
| 250 | The double-star character `**` is supported by default, unless the
|
|---|
| 251 | `noglobstar` flag is set. This is supported in the manner of bsdglob
|
|---|
| 252 | and bash 4.1, where `**` only has special significance if it is the only
|
|---|
| 253 | thing in a path part. That is, `a/**/b` will match `a/x/y/b`, but
|
|---|
| 254 | `a/**b` will not.
|
|---|
| 255 |
|
|---|
| 256 | If an escaped pattern has no matches, and the `nonull` flag is set,
|
|---|
| 257 | then minimatch.match returns the pattern as-provided, rather than
|
|---|
| 258 | interpreting the character escapes. For example,
|
|---|
| 259 | `minimatch.match([], "\\*a\\?")` will return `"\\*a\\?"` rather than
|
|---|
| 260 | `"*a?"`. This is akin to setting the `nullglob` option in bash, except
|
|---|
| 261 | that it does not resolve escaped pattern characters.
|
|---|
| 262 |
|
|---|
| 263 | If brace expansion is not disabled, then it is performed before any
|
|---|
| 264 | other interpretation of the glob pattern. Thus, a pattern like
|
|---|
| 265 | `+(a|{b),c)}`, which would not be valid in bash or zsh, is expanded
|
|---|
| 266 | **first** into the set of `+(a|b)` and `+(a|c)`, and those patterns are
|
|---|
| 267 | checked for validity. Since those two are valid, matching proceeds.
|
|---|