javascript Javascript正则表达式匹配完全限定域名,无协议,可选子域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16463666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:46:35  来源:igfitidea点击:

Javascript regex to match fully qualified domain name, without protocol, optional subdomain

javascriptregex

提问by Jared Eitnier

I haven't been able to find this one and what I'm trying isn't quite working out for me.

我一直无法找到这个,而且我正在尝试的方法对我来说并不完全有效。

I want to match only domains that:

我只想匹配以下域:

  • don't contain a protocol (http, https, ftp)
  • optionally include a subdomain
  • don't startwith a hyphen but cancontain a hyphen
  • 不包含协议(http、https、ftp)
  • 可选地包括一个子域
  • 不要以连字符开头,但可以包含连字符

Example domains that would match:

匹配的示例域:

  • domain.com
  • example.domain.com
  • example.domain-hyphen.com
  • www.domain.com
  • example.museum
  • 域名.com
  • example.domain.com
  • example.domain-hyphen.com
  • www.domain.com
  • 例子.博物馆

Example domains that would notmatch:

匹配的示例域:

  • http://example.com
  • subdomain.-example.com
  • example.com/parameter
  • example.com?anything
  • www.subdomain.domain.com
  • http://example.com
  • 子域.-example.com
  • 例子.com/参数
  • example.com?任何东西
  • www.subdomain.domain.com

What I've currently got:

我目前所拥有的:

/^(?!:\/\/)(^[a-zA-Z0-9])?.[a-zA-Z0-9-]+\.[a-zA-Z]{2,6}?$/i

It's not matching protocols, allowing hyphen inside the domain, not allowing trailing characters after the TLD, and is allowing a subdomain (but only 1 character).

它不匹配协议,允许在域内使用连字符,不允许在 TLD 后使用尾随字符,并且允许使用子域(但只有 1 个字符)。

I still need to allow subdomains of any length, not allow www.subdomain.domain.com and not allow a leading hyphen.

我仍然需要允许任何长度的子域,不允许 www.subdomain.domain.com 并且不允许前导连字符。

回答by dda

Try

尝试

/^(?!:\/\/)([a-zA-Z0-9]+\.)?[a-zA-Z0-9][a-zA-Z0-9-]+\.[a-zA-Z]{2,6}?$/i

回答by Bergi

Let's analyse your regex:

让我们分析您的正则表达式:

^(?!:\/\/)
^(?!:\/\/)

This is pretty useless. While it indicates the intention of the regex, it's unnecessary since the following characters are not allowed to contain slashes anyway.

这很没用。虽然它表明了正则表达式的意图,但它是不必要的,因为无论如何都不允许以下字符包含斜杠。

(^[a-zA-Z0-9])?.
(^[a-zA-Z0-9])?.

I think you wanted this to be ^([a-zA-Z0-9]+\.)?. Your dot is not escaped, and would be preceded by only one optional character at the string beginning.

我想你想要这个^([a-zA-Z0-9]+\.)?。您的点不会被转义,并且在字符串开头只会有一个可选字符。

[a-zA-Z0-9-]+
[a-zA-Z0-9-]+

If you want this not to begin with a hyphen, you either could use a negative lookahead or better just [a-zA-Z0-9][a-zA-Z0-9-]*.

如果您不希望它以连字符开头,您可以使用否定前瞻或更好的[a-zA-Z0-9][a-zA-Z0-9-]*.

\.[a-zA-Z]{2,6}?
\.[a-zA-Z]{2,6}?

Not sure what the question mark does here. There's no backtracking anyway?

不知道问号在这里做什么。反正没有回溯?

/i
/i

This renders the explicit [a-zA-Z]useless, one would be enough. Or omit the iflag.

这使得显式[a-zA-Z]无用,一个就足够了。或者省略i标志。

All these things together, we will end up with

所有这些事情在一起,我们将结束

/^([a-z0-9]+\.)?[a-z0-9][a-z0-9-]*\.[a-z]{2,6}$/i

回答by Dharmesh Hadiyal

Try this:

试试这个:

^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$

Demo

演示

回答by Michal Aron

The RegEx I came up with while doing the Hostname/FQDN validation in Javascript:

我在 Javascript 中进行主机名/FQDN 验证时想到的正则表达式:

FQDN:

域名:

^(?!:\/\/)(?!.{256,})(([a-z0-9][a-z0-9_-]*?\.)+?[a-z]{2,6}?)$/i

^(?!:\/\/)(?!.{256,})(([a-z0-9][a-z0-9_-]*?\.)+?[a-z]{2,6}?)$/i

Hostname or FQDN

主机名或 FQDN

^(?!:\/\/)(?!.{256,})(([a-z0-9][a-z0-9_-]*?)|([a-z0-9][a-z0-9_-]*?\.)+?[a-z]{2,6}?)$/i

^(?!:\/\/)(?!.{256,})(([a-z0-9][a-z0-9_-]*?)|([a-z0-9][a-z0-9_-]*?\.)+?[a-z]{2,6}?)$/i

Both expressions use lookahead to check the total string length, which can be up to 255 characters. They also do a lazy check .{x,y}?.

这两个表达式都使用前瞻来检查总字符串长度,最多可以包含 255 个字符。他们还做一个懒惰的检查.{x,y}?

Note that it is using case insensitive match /i.

请注意,它使用不区分大小写的 match /i