什么是匹配没有子域的有效域名的正则表达式? [英] What is a regular expression which will match a valid domain name without a subdomain?

查看:73
本文介绍了什么是匹配没有子域的有效域名的正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证一个域名:

google.com

stackoverflow.com

所以是最原始形式的域 - 甚至不是像 www 这样的子域.

So a domain in its rawest form - not even a subdomain like www.

  1. 字符只能是a-z |A-Z |0-9句点(.) 和 破折号(-)
  2. 域名部分不应以破折号 (-) 开头或结尾(例如 -google-.com)
  3. 域名部分的长度应在 1 到 63 个字符之间
  4. 扩展名 (TLD) 现在可以是 #1 规则下的任何内容,我稍后可能会根据列表验证它们,但它应该是 1 个或更多字符
  1. Characters should only be a-z | A-Z | 0-9 and period(.) and dash(-)
  2. The domain name part should not start or end with dash (-) (e.g. -google-.com)
  3. The domain name part should be between 1 and 63 characters long
  4. The extension (TLD) can be anything under #1 rules for now, I may validate them against a list later, it should be 1 or more characters though

TLD 显然是 2-6 个字符

没有.4 修订版: TLD 实际上应该被标记为子域",因为它应该包括 .co.uk 之类的东西——我想唯一可能的验证(除了对照列表进行检查)将是在那里的第一个点之后"规则 #1 下应该是一个或多个字符

no. 4 revised: TLD should actually be labelled "subdomain" as it should include things like .co.uk -- I would imagine the only validation possible (apart from checking against a list) would be 'after the first dot there should be one or more characters under rules #1

非常感谢,相信我,我确实尝试过!

Thanks very much, believe me I did try!

推荐答案

嗯,鉴于您的具体要求,它非常简单 比看起来更狡猾(见评论):

Well, it's pretty straightforward a little sneakier than it looks (see comments), given your specific requirements:

/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$/

但请注意,这会拒绝很多有效域名.

But note this will reject a lot of valid domains.

这篇关于什么是匹配没有子域的有效域名的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆