前两天,咱们学习了Node.js中模式匹配文件列表的 glob 和 glob 的加强版globby,今天,咱们将了解 glob 的基础库: minimatch,用来模式匹配字符串的库。node
其实,glob库支持的的各类模式都来自于minimatch。git
const minimatch = require("minimatch") minimatch("bar.foo", "*.foo") // true minimatch("bar.foo", "*.bar") // false minimatch("bar.foo", "*.+(bar|foo)", { debug: true }) // true
要注意,minimatch的匹配模式并不是是正则表达式,具体支持以下:github
*
匹配0到多个字符?
匹配一个字符[...]
匹配一个字符列表,相似正则表达式的字符列表!(pattern|pattern|pattern)
反向匹配括号内的模式?(pattern|pattern|pattern)
匹配0或1个括号内的模式+(pattern|pattern|pattern)
匹配至少1个括号内的模式*(pattern|pattern|pattern)
匹配0到多个括号内的模式@(pattern|pat*|pat?erN)
精确匹配括号内的模式**
匹配0到多个子目录,递归匹配子目录https://github.com/isaacs/nod...正则表达式