简体   繁体   English

URL 正则表达式模式

[英]URL regular expression pattern

I would like to parse URLs with Regular Expressions and find a pattern that matches with https://*.global .我想用正则表达式解析 URL 并找到与https://*.global匹配的模式。

Here is my URL test string on regex101.这是我在 regex101 上的 URL 测试字符串。

Ideally, the regex would return https://app8.global instead of cover other https string.理想情况下,正则表达式将返回https://app8.global而不是覆盖其他https字符串。

 const URL = `https://temp/"https://app8.global"https://utility.localhost/`; const regex = /https:\\/\\/(.+?)\\.global(\\/|'|"|`)/gm; const found = URL.match(regex); console.log(found);

How would I manipulate the regex so it will return the https://*.global ?我将如何操作正则表达式,使其返回https://*.global

First of all, you need to exclude slashes from the starting part, otherwise it'll match things from the previous url:首先,您需要从起始部分排除斜杠,否则它将与上一个 url 中的内容匹配:

const regex = /https:\/\/([^\/]+?)\.global(\/|'|"|`)/gm;

Now, you can convert the weird 4 character or with a character group:现在,您可以转换奇怪的 4 个字符或使用字符组:

const regex = /https:\/\/([^\/]+?)\.global[\/'"`]/gm;

And now you can get the matches and trim off that last character:现在您可以获取匹配项并修剪掉最后一个字符:

const matches = URL.match(regex).map(v => v.slice(0, -1));

Then, matches would evaluate to ["https://app8.global"] .然后, matches将评估为["https://app8.global"]

Using Group RegExp.$1使用 Group RegExp.$1

 const URL = `https://temp/"https://app8.global"https://utility.localhost/`; const regex = /(https:\\/\\/([^\\/]+?)\\.global[\\/'"`])/; const found = URL.match(regex); console.log(RegExp.$1);

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

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