Search code examples
glob

Glob Pattern Matching String in either folder or file name


I need to match the word "cache" in either a folder or filename, and it also has to end in .test.js

Example:

    /Users/doge/lib/cache/index.js // should not match
    /Users/doge/lib/cache/index.test.js // should match
    /Users/doge/lib/something/index.test.js // should not match
    /Users/doge/lib/something/cache.test.js // should match

I've tried /**/*cache*.test.js but that doens't seem to work.


Solution

  • How about **/{*cache*/*,*cache*}.test.js ? This is assuming you are using minimatch library (which is likely if you are working with JavaScript). Try using globster.xyz to test and improve your pattern.

    In the pattern I mentioned {} are a feature called brace expansion. This pattern will be expanded into two separate patterns: one for matching a directory name and the other for matching filename **/*cache*/*.test.js, **/*cache*.test.js. This is still not perfect because the folder containing cache will have to be exactly one level above the file itself but maybe it works for you.