最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex Exclude folder from path - Stack Overflow

programmeradmin1浏览0评论

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
 |  Show 2 more ments

2 Answers 2

Reset to default 6

Probably 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: $
发布评论

评论列表(0)

  1. 暂无评论