简体   繁体   English

如何不使用^和Capture组使用正则表达式?

[英]How to use regex with not ^ and capture groups?

How do I use the Not operator ^ with the capture groups in regex. 如何将Not运算符^与正则表达式中的捕获组一起使用。

Example: I have two URLs coming in http://api.example.com and http://example.com 示例:我在http://api.example.comhttp://example.com中有两个URL

How would I make a capture group on all URLs that do not contain api . 如何在不包含api所有URL上建立捕获组。

How I thought this would look is var notAPI = /(^api)/; 我认为这看起来是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. 看看这个答案 -它也解释了它是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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