Search code examples
javascriptregexregex-lookarounds

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.


Solution

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