prgrmr
prgrmr

Reputation: 852

Regex Exclude folder from path

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.

Upvotes: 2

Views: 12057

Answers (2)

Chris Sprague
Chris Sprague

Reputation: 3574

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: $

Upvotes: 0

Kevin Qian
Kevin Qian

Reputation: 2720

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)))

Upvotes: 6

Related Questions