isaacs / minimatch

a glob matcher in javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Globstar not matching zero

diegodlh opened this issue · comments

According to the docs, minimatch should behave like Bash 4.1.

According to this comment, the /path/**/*.html pattern should match /path/to/page.html, /path/to/web/page.html, and also /path/page.html, for Bash 4 and above. This works in my Bash 4.4.20.

However, if I use minimatch to create a RegExp for the pattern above:

const makeRe = require("minimatch").makeRe;
const regex = makeRe("/path/**/*.html");

the resulting RegExp seems to be ^(?:\/path\/(?:(?!(?:\/|^)\.).)*?\/(?!\.)(?=.)[^\/]*?\.html)$, which although matches /path/to/page.html and /path/to/web/page.html, it doesn't match /path/page.html

This is a known limitation of the regular expression returned by makeRe. Any approach to matching globstar in a re would result in a redos vulnerability or require negative lookbehind (which JS doesn't support).

You have to use the minimatch function itself to match globstar patterns.

> const mm = require('minimatch')
undefined
> mm('path/page.html', 'path/**/*.html')
true
> mm('path/home/page.html', 'path/**/*.html')
true
> mm('path/www/home/page.html', 'path/**/*.html')
true

Actually, I lied, this can be done by just doing the lookahead/behind in JavaScript when building the regexp. One sec.

Works in 4.2.0 now.