isaacs / minimatch

a glob matcher in javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Match folder with optional leading and trailing folders

ade-verd opened this issue · comments

Hi,

I would like to match every paths that contains foo in the following paths:

../../foo/bar/abc
../../foo/bar
../../foo
../foo
./foo
foo

I tried to use the pattern **foo** but it didn't work.

While using the debug option, I can see the regex: '(?!\\.)(?=.)[^/]*?[^/]*?foo[^/]*?[^/]*?'
This regex seems matching all my paths. So I'm not sure to understand.

My closest solution was to use the pattern foo associated with { matchBase: true }.
But it doesn't work when foo has trailing path.

Do you have any idea ?


Note: I am not able to use regex, because I am indirectly using minimatch with eslint-plugin-import

** is only relevant when it's the only contents of a path portion. Try ./**/foo/**

Thanks for your answer @isaacs
But it doesn't work either

You can find here some simple tests: https://onecompiler.com/nodejs/3y2p59aqp

any update on this? I also have a very similar use case to OP

Oh, you want to go up the folder tree? In that case, you have to provide the .. portions explicitly. ** will not match the . or ../ directory entries, by design, because otherwise $ echo **/foo on your shell would have to search your entire computer.

For the case described, the pattern '{,./,../,../../}**/foo{,/**}' will do what you're asking for. But if you want to match any arbitrary number of ../ portions, sorry, this isn't going to be able to do that.

You could also just remove the ../ portions prior to testing, or use path.join() to remove them from everywhere.

But really, this should be a big red flag. If you're hoping to match .. path portions with a glob, you're probably opening yourself up to some security issues, or at least, very weird behavior. What if the path is ./foo/../bar? If **/foo/** matched that, then you'd be matching the effective path of ./bar, which doesn't have foo in it.

Back up a step, and see if there's another way around. How did you end up having this use case? Something is probably weird there, or could be addressed in a more elegant way. If you share the situation, I'd be happy to help think it through if I can.