前兩天,咱們學習了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...正則表達式