0

How do I use the Not operator ^ with the capture groups in regex.

Example: I have two URLs coming in http://api.example.com and http://example.com

How would I make a capture group on all URLs that do not contain api.

How I thought this would look is var notAPI = /(^api)/;

ccjmne
  • 9,333
  • 3
  • 47
  • 62
Armeen Moon
  • 18,061
  • 35
  • 120
  • 233
  • 1
    Use `indexOf`. `if (url.indexOf('api')) === -1` or `!/api/.test(url)` – Tushar Feb 05 '16 at 18:03
  • Possible duplicate of [Regular expression that doesn't contain certain string](http://stackoverflow.com/questions/717644/regular-expression-that-doesnt-contain-certain-string) – randers Feb 05 '16 at 18:12

1 Answers1

1

Regex only has negative lookaheads and lookbehinds. Their spirit is: you only look, you don't move forward.

This is a negative lookahead:

(?!nomatch)

And this is a negative lookbehind:

(?<!nomatch)

You can incorporate these lookarounds into your regex like this:

var notAPI = /^((?!api).)*$/;

Take a look at this answer - it also explains how it works.

randers
  • 5,031
  • 5
  • 37
  • 64