Regex 失败子域的正则表达式

Regex 失败子域的正则表达式,regex,iis,regex-lookarounds,regex-group,regex-greedy,Regex,Iis,Regex Lookarounds,Regex Group,Regex Greedy,基本上,我想检查一个没有子域的有效URL。我似乎找不出正确的正则表达式 应匹配的URL示例: example.com www.example.com example.co.uk example.com/page example.com?key=value 不应匹配的URL示例: test.example.com sub.test.example.com 在这里,我们将从一个表达式开始,该表达式在右侧以.com或.co.uk等为界,如果需要,我们将向左滑动以收集所有非点字符,添加可选的www和ht

基本上,我想检查一个没有子域的有效URL。我似乎找不出正确的正则表达式

应匹配的URL示例:

example.com www.example.com example.co.uk example.com/page example.com?key=value 不应匹配的URL示例:

test.example.com sub.test.example.com
在这里,我们将从一个表达式开始,该表达式在右侧以.com或.co.uk等为界,如果需要,我们将向左滑动以收集所有非点字符,添加可选的www和https,然后添加一个将使所有子域失败的起始字符^:

^(https?:\/\/)?(www\.)?([^.]+)(\.com|\.co\.uk)(.+|)$
可以将其他TLD添加到此捕获组:

(\.com|\.co\.uk|\.net|\.org|\.business|\.edu|\.careers|\.coffee|\.college)
表达式可以修改为:

^(https?:\/\/)?(www\.)?([^.]+)(\.com|\.co\.uk|\.net|\.org|\.business|\.edu|\.careers|\.coffee|\.college)(.+|)$
灵活性 由于这是一个验证表达式,所以我想不出什么东西可以使TLD过于灵活。例如,如果我们将其简化为:

^(https?:\/\/)?(www\.)?([^.]+)(\.[a-z]+)(\.uk?)?[a-z?=\/]+$
它可能适用于问题中列出的URL,但也会传递:

example.example
这是无效的。我们只能使用以下表达式:

^(https?:\/\/)?(www\.)?([^.]+)(\.[a-z]+)(\.uk?)?[a-z?=\/]+$
如果我们知道我们传递的内容,它已经是一个URL

演示 此代码段仅显示了捕获组的工作方式:

const regex=/^https?:\/\/?www.?[^.]+\.com\\.co\.uk.+\$/gm; const str=`example.com www.example.com example.co.uk example.com/page example.com?key=value test.example.com sub.test.example.com`; 让m; 而m=regex.execstr!==空的{ //这是避免具有零宽度匹配的无限循环所必需的 如果m.index==regex.lastIndex{ regex.lastIndex++; } //可以通过'm`-变量访问结果。 m、 forEachmatch,groupIndex=>{ log`find match,group${groupIndex}:${match}`; };
}除了.com之外,您如何捕获不同的TLD?我尝试了^https?:\/\/?www\.?[^.]+\.[^.]+.+.+\$,但这一切都很匹配。我希望有一种方法可以使它更灵活,这样我就不会在每次引入新TLD时都进行重新配置,尽管我并不经常看到这种情况发生。