I have a bunch of files like these
/foo/bar/specs/d.js
/foo/bar/spec/d.js
/foo/bar/specs/v.js
/foo/bar/specs/v.js
/node_modules/bar/specs/v.js
I need a regex that will exclude everything under node_modules
Something like this:
(?!node_modules)\/.*\/specs\/.*\.js
Unfortunately it isnt working
Appreciate your help.
I have a bunch of files like these
/foo/bar/specs/d.js
/foo/bar/spec/d.js
/foo/bar/specs/v.js
/foo/bar/specs/v.js
/node_modules/bar/specs/v.js
I need a regex that will exclude everything under node_modules
Something like this:
(?!node_modules)\/.*\/specs\/.*\.js
Unfortunately it isnt working
Appreciate your help.
Share Improve this question asked Mar 12, 2018 at 23:29 prgrmrprgrmr 8524 gold badges16 silver badges31 bronze badges 7-
3
Here, take this sword. It will serve you well walking through the
regex
forest. – Learn on hard way Commented Mar 12, 2018 at 23:32 - @Learnonhardway it's a blunt one :) – vitaly-t Commented Mar 12, 2018 at 23:33
- We need more information about your specific use case in order to remend an appropriate solution to your problem. It seems likely that you're overthinking the problem. Is this pattern matching for a preprocessor such as webpack? – Jason Warta Commented Mar 12, 2018 at 23:36
- @JasonWarta am just loading up a bunch of files from diff dirs.... need to exclude the ones under node_modules – prgrmr Commented Mar 12, 2018 at 23:37
- 1 @Learnonhardway I actually love the sword, thanks – Kevin Qian Commented Mar 12, 2018 at 23:51
2 Answers
Reset to default 6Probably something like this?
let arr = [
'/foo/bar/specs/d.js',
'/foo/bar/specs/d.js',
'/foo/bar/specs/v.js',
'/foo/bar/specs/v.js',
'/node_modules/bar/specs/v.js'
]
arr.forEach(s => console.log(/^\/(?!node_modules).*\/.*\/specs\/.*\.js/.test(s)))
Here's a regex that gets .ts and .js files that are not in the .git or node_modules folder:
^(?!.*\/(node_modules|\.git)\/).*(.ts|.js)$
Explanation:
- Start of string:
^
- Not in group:
(?!.*\/(node_modules|\.git)\/)
- Include file types group:
(.ts|.js)
- End of string:
$