You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript子域名验证正则表达式需求:排除顶级域名

Validating Subdomains (Not Top-Level/Registered Domains) in JavaScript

Got it, let's break down what you need: you want a regex that rejects "base" registered domains like domain.uk or domain.ac.uk, but accepts their subdomains like sub.domain.uk or sub.domain.ac.uk.

First, let's clarify the core rule from your examples:

  • Reject domains that are directly registered under a public suffix (like .uk or .ac.uk)
  • Accept domains that have an extra subdomain prefix added to those registered base domains

The challenge here is that public suffixes can be single-segment (.uk, .com) or multi-segment (.ac.uk, .com.au). A pure regex can't perfectly cover every possible public suffix (since the list is maintained by organizations like Mozilla), but we can build a solid approximation that works for your examples and most common cases.

The Regex

Here's a tailored regex for JavaScript:

const subdomainRegex = /^(?!^[a-zA-Z0-9-]+\.[a-zA-Z0-9-]{2,}(\.[a-zA-Z0-9-]{2,})?$)[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+){2,}$/;

How It Works

Let's break down the components:

  1. Negative Lookahead ((?!...)): This part rejects any domain that matches the "base registered domain" pattern:

    • ^[a-zA-Z0-9-]+: The main domain label (e.g., domain in domain.uk)
    • \.[a-zA-Z0-9-]{2,}: A top-level suffix (e.g., .uk, .com)
    • (\.[a-zA-Z0-9-]{2,})?$: Optional second segment for multi-part suffixes (e.g., .ac in .ac.uk)
      This ensures we exclude domains like domain.uk (2 segments) and domain.ac.uk (3 segments).
  2. Main Matching Pattern:

    • [a-zA-Z0-9-]+: The subdomain label (e.g., sub in sub.domain.uk)
    • (\.[a-zA-Z0-9-]+){2,}: At least two additional segments (the base registered domain, which can be 2 or more segments)
      This ensures we accept domains like sub.domain.uk (3 segments) and sub.domain.ac.uk (4 segments).

Testing It Out

Let's verify with your examples:

  • domain.uk → ❌ Rejected (matches the base domain pattern)
  • sub.domain.uk → ✅ Accepted
  • domain.ac.uk → ❌ Rejected (matches the base domain pattern)
  • sub.domain.ac.uk → ✅ Accepted

Notes on Limitations

  • This regex uses a general pattern for public suffixes. If you need to cover rare or custom multi-segment suffixes, you'd need to expand the negative lookahead to target those specific cases.
  • It doesn't handle edge cases like internationalized domain names (IDNs) unless you add support for Unicode characters (using \p{L} instead of a-zA-Z, but note that JavaScript regex requires the u flag for Unicode property escapes).

Example Usage

function isSubdomain(hostname) {
  const subdomainRegex = /^(?!^[a-zA-Z0-9-]+\.[a-zA-Z0-9-]{2,}(\.[a-zA-Z0-9-]{2,})?$)[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+){2,}$/;
  return subdomainRegex.test(hostname);
}

// Test cases
console.log(isSubdomain("domain.uk")); // false
console.log(isSubdomain("sub.domain.uk")); // true
console.log(isSubdomain("domain.ac.uk")); // false
console.log(isSubdomain("sub.domain.ac.uk")); // true

内容的提问来源于stack exchange,提问作者HA Luca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:01:02