25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 7.0 KiB

10 달 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # minimatch
  2. A minimal matching utility.
  3. [![Build Status](https://travis-ci.org/isaacs/minimatch.svg?branch=master)](http://travis-ci.org/isaacs/minimatch)
  4. This is the matching library used internally by npm.
  5. It works by converting glob expressions into JavaScript `RegExp`
  6. objects.
  7. ## Usage
  8. ```javascript
  9. var minimatch = require("minimatch")
  10. minimatch("bar.foo", "*.foo") // true!
  11. minimatch("bar.foo", "*.bar") // false!
  12. minimatch("bar.foo", "*.+(bar|foo)", { debug: true }) // true, and noisy!
  13. ```
  14. ## Features
  15. Supports these glob features:
  16. * Brace Expansion
  17. * Extended glob matching
  18. * "Globstar" `**` matching
  19. See:
  20. * `man sh`
  21. * `man bash`
  22. * `man 3 fnmatch`
  23. * `man 5 gitignore`
  24. ## Minimatch Class
  25. Create a minimatch object by instantiating the `minimatch.Minimatch` class.
  26. ```javascript
  27. var Minimatch = require("minimatch").Minimatch
  28. var mm = new Minimatch(pattern, options)
  29. ```
  30. ### Properties
  31. * `pattern` The original pattern the minimatch object represents.
  32. * `options` The options supplied to the constructor.
  33. * `set` A 2-dimensional array of regexp or string expressions.
  34. Each row in the
  35. array corresponds to a brace-expanded pattern. Each item in the row
  36. corresponds to a single path-part. For example, the pattern
  37. `{a,b/c}/d` would expand to a set of patterns like:
  38. [ [ a, d ]
  39. , [ b, c, d ] ]
  40. If a portion of the pattern doesn't have any "magic" in it
  41. (that is, it's something like `"foo"` rather than `fo*o?`), then it
  42. will be left as a string rather than converted to a regular
  43. expression.
  44. * `regexp` Created by the `makeRe` method. A single regular expression
  45. expressing the entire pattern. This is useful in cases where you wish
  46. to use the pattern somewhat like `fnmatch(3)` with `FNM_PATH` enabled.
  47. * `negate` True if the pattern is negated.
  48. * `comment` True if the pattern is a comment.
  49. * `empty` True if the pattern is `""`.
  50. ### Methods
  51. * `makeRe` Generate the `regexp` member if necessary, and return it.
  52. Will return `false` if the pattern is invalid.
  53. * `match(fname)` Return true if the filename matches the pattern, or
  54. false otherwise.
  55. * `matchOne(fileArray, patternArray, partial)` Take a `/`-split
  56. filename, and match it against a single row in the `regExpSet`. This
  57. method is mainly for internal use, but is exposed so that it can be
  58. used by a glob-walker that needs to avoid excessive filesystem calls.
  59. All other methods are internal, and will be called as necessary.
  60. ### minimatch(path, pattern, options)
  61. Main export. Tests a path against the pattern using the options.
  62. ```javascript
  63. var isJS = minimatch(file, "*.js", { matchBase: true })
  64. ```
  65. ### minimatch.filter(pattern, options)
  66. Returns a function that tests its
  67. supplied argument, suitable for use with `Array.filter`. Example:
  68. ```javascript
  69. var javascripts = fileList.filter(minimatch.filter("*.js", {matchBase: true}))
  70. ```
  71. ### minimatch.match(list, pattern, options)
  72. Match against the list of
  73. files, in the style of fnmatch or glob. If nothing is matched, and
  74. options.nonull is set, then return a list containing the pattern itself.
  75. ```javascript
  76. var javascripts = minimatch.match(fileList, "*.js", {matchBase: true}))
  77. ```
  78. ### minimatch.makeRe(pattern, options)
  79. Make a regular expression object from the pattern.
  80. ## Options
  81. All options are `false` by default.
  82. ### debug
  83. Dump a ton of stuff to stderr.
  84. ### nobrace
  85. Do not expand `{a,b}` and `{1..3}` brace sets.
  86. ### noglobstar
  87. Disable `**` matching against multiple folder names.
  88. ### dot
  89. Allow patterns to match filenames starting with a period, even if
  90. the pattern does not explicitly have a period in that spot.
  91. Note that by default, `a/**/b` will **not** match `a/.d/b`, unless `dot`
  92. is set.
  93. ### noext
  94. Disable "extglob" style patterns like `+(a|b)`.
  95. ### nocase
  96. Perform a case-insensitive match.
  97. ### nonull
  98. When a match is not found by `minimatch.match`, return a list containing
  99. the pattern itself if this option is set. When not set, an empty list
  100. is returned if there are no matches.
  101. ### matchBase
  102. If set, then patterns without slashes will be matched
  103. against the basename of the path if it contains slashes. For example,
  104. `a?b` would match the path `/xyz/123/acb`, but not `/xyz/acb/123`.
  105. ### nocomment
  106. Suppress the behavior of treating `#` at the start of a pattern as a
  107. comment.
  108. ### nonegate
  109. Suppress the behavior of treating a leading `!` character as negation.
  110. ### flipNegate
  111. Returns from negate expressions the same as if they were not negated.
  112. (Ie, true on a hit, false on a miss.)
  113. ### partial
  114. Compare a partial path to a pattern. As long as the parts of the path that
  115. are present are not contradicted by the pattern, it will be treated as a
  116. match. This is useful in applications where you're walking through a
  117. folder structure, and don't yet have the full path, but want to ensure that
  118. you do not walk down paths that can never be a match.
  119. For example,
  120. ```js
  121. minimatch('/a/b', '/a/*/c/d', { partial: true }) // true, might be /a/b/c/d
  122. minimatch('/a/b', '/**/d', { partial: true }) // true, might be /a/b/.../d
  123. minimatch('/x/y/z', '/a/**/z', { partial: true }) // false, because x !== a
  124. ```
  125. ### allowWindowsEscape
  126. Windows path separator `\` is by default converted to `/`, which
  127. prohibits the usage of `\` as a escape character. This flag skips that
  128. behavior and allows using the escape character.
  129. ## Comparisons to other fnmatch/glob implementations
  130. While strict compliance with the existing standards is a worthwhile
  131. goal, some discrepancies exist between minimatch and other
  132. implementations, and are intentional.
  133. If the pattern starts with a `!` character, then it is negated. Set the
  134. `nonegate` flag to suppress this behavior, and treat leading `!`
  135. characters normally. This is perhaps relevant if you wish to start the
  136. pattern with a negative extglob pattern like `!(a|B)`. Multiple `!`
  137. characters at the start of a pattern will negate the pattern multiple
  138. times.
  139. If a pattern starts with `#`, then it is treated as a comment, and
  140. will not match anything. Use `\#` to match a literal `#` at the
  141. start of a line, or set the `nocomment` flag to suppress this behavior.
  142. The double-star character `**` is supported by default, unless the
  143. `noglobstar` flag is set. This is supported in the manner of bsdglob
  144. and bash 4.1, where `**` only has special significance if it is the only
  145. thing in a path part. That is, `a/**/b` will match `a/x/y/b`, but
  146. `a/**b` will not.
  147. If an escaped pattern has no matches, and the `nonull` flag is set,
  148. then minimatch.match returns the pattern as-provided, rather than
  149. interpreting the character escapes. For example,
  150. `minimatch.match([], "\\*a\\?")` will return `"\\*a\\?"` rather than
  151. `"*a?"`. This is akin to setting the `nullglob` option in bash, except
  152. that it does not resolve escaped pattern characters.
  153. If brace expansion is not disabled, then it is performed before any
  154. other interpretation of the glob pattern. Thus, a pattern like
  155. `+(a|{b),c)}`, which would not be valid in bash or zsh, is expanded
  156. **first** into the set of `+(a|b)` and `+(a|c)`, and those patterns are
  157. checked for validity. Since those two are valid, matching proceeds.