admin管理员组文章数量:1316351
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:
$
本文标签: javascriptRegex Exclude folder from pathStack Overflow
版权声明:本文标题:javascript - Regex Exclude folder from path - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742002759a2411348.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论