简体   繁体   中英

How to use regex with not ^ and capture groups?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM