1

The input is an absolute path for example:

/usr/local/lib/node_modules/normalize
/usr/local/lib/node_modules/bobcat/index.js

I want to create a regex that matches EVERYTHING except when "bobcat" was found in the string:

This is what I got to match everything:

var pattern = /node_modules/g;
var matches = pattern.test(input);

How can I do this?

2
  • 1
    It's better to use two simple regular expressions than a single complex one. Commented Apr 4, 2016 at 19:47
  • how about a negation? var pattern = /bobcat/, matches = !pattern.test(input); Commented Apr 4, 2016 at 19:54

2 Answers 2

2

You can use a negative lookahead regex:

/node_modules(?!.*\/bobcat\/)/g

RegEx Demo

(?!.*\/bobcat\/) is negative lookahead that will fail the match if /bobcat/ comes after node_modules.

Sign up to request clarification or add additional context in comments.

Comments

1

Make an if/else statement and check if /bobycat/ matches. Put your code in the else part.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.