NPM酷库:minimatch,模式匹配字符串

前两天,我们学习了Node.js中模式匹配文件列表的 glob 和 glob 的增强版globby,今天,我们将了解 glob 的基础库: minimatch,用来模式匹配字符串的库。

其实,glob库支持的的各种模式都来自于minimatch。

minimatch 的用法

const minimatch = require("minimatch")

minimatch("bar.foo", "*.foo") // true
minimatch("bar.foo", "*.bar") // false
minimatch("bar.foo", "*.+(bar|foo)", { debug: true }) // true

minimatch 支持的通配符模式

要注意,minimatch的匹配模式并非是正则表达式,具体支持如下:

  • * 匹配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…

https://github.com/isaacs/min…

https://en.wikipedia.org/wiki…

    原文作者:脉冲云_梁兴臣
    原文地址: https://segmentfault.com/a/1190000012795735
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞