Domain with TLD Regex
Validates a domain name that ends in a letters-only top-level domain of two or more characters, such as example.com, example.co.uk, or www.example.com.
Regex Pattern
^[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z]{2,}$Pattern Breakdown
Hover over a token to see what it does.
| Token | Meaning |
|---|---|
| ^ | Anchors the match to the start of the string |
| [a-zA-Z0-9-]+ | The first domain label: one or more letters, digits, or hyphens |
| (\.[a-zA-Z0-9-]+)* | Zero or more additional dot-separated labels, allowing subdomains or multi-part suffixes |
| \. | Literal dot immediately before the top-level domain |
| [a-zA-Z]{2,} | The top-level domain: two or more letters only, no digits or hyphens |
| $ | Anchors the match to the end of the string |
Detailed Explanation
What it does
This pattern checks that a string looks like a domain name with a valid top-level domain: at least one label followed by a dot and a final segment made only of letters. It accepts bare domains, subdomains, and multi-part TLDs alike.
Why it works
The first label and any number of following (\.[a-zA-Z0-9-]+)* labels absorb everything except the final segment, and the pattern forces the very last segment to be letters-only with a minimum length of two, which is how real top-level domains are structured. This distinguishes example.com from something like example.c or example.com123, where the trailing segment is too short or contains digits.
Common use cases
- Validating a domain name field on a domain registration or DNS management form
- Quick client-side sanity checks before sending a domain to a WHOIS or DNS lookup API
- Filtering a list of strings to find the ones that plausibly represent domain names
- Pre-validating the host portion of an email address or URL before deeper parsing
Edge cases
- Multi-part TLDs like .co.uk are accepted because the pattern does not need to know the exact suffix, only that the final segment is letters-only
- Numeric-only labels like 123.example.com are accepted since digits are allowed in non-final labels
- A trailing TLD with digits, like example.com123, is correctly rejected since the final segment requires letters only
- A domain without any dot, like localhost, is rejected because the pattern requires at least one dot before the TLD
Limitations
- Allows a label to start or end with a hyphen, which is technically invalid under DNS labeling rules
- Does not check the Public Suffix List, so it cannot confirm a TLD like .com or .co.uk actually exists
- Does not enforce the 63-character-per-label or 253-character total length limits from the DNS specification
- Does not support internationalized domain names unless they are already punycode-encoded
Interactive Tester
Edit the pattern or text below — matching runs live in your browser.
Test Cases
Editable — add your own inputs to see if they pass.
| Input | Expected | Result | |
|---|---|---|---|
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass |
Language Variants
Production-ready examples in 12 languages.
const domainTldRegex = /^[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z]{2,}$/;
console.log(domainTldRegex.test('example.com')); // trueCommon Mistakes
Allowing digits in the final TLD segment, which would let invalid strings like example.com123 pass
Fix: Restrict the last segment to [a-zA-Z]{2,} only, with no digits or hyphens
Requiring exactly one dot, which breaks on multi-part TLDs like .co.uk or subdomains
Fix: Use a repeatable (\.[a-zA-Z0-9-]+)* group for the middle labels so any number of dots is allowed before the final TLD
Assuming a match means the domain is registered and resolvable
Fix: Follow up a successful regex match with an actual DNS lookup if reachability matters
Performance Notes
- Anchoring with ^ and $ avoids scanning for matches at multiple positions in longer input
- The repeated group (\.[a-zA-Z0-9-]+)* has no nested overlapping quantifiers, so it does not risk catastrophic backtracking
- This is one of the simplest patterns in this reference, with linear-time matching for realistic domain-length strings
Browser Compatibility
| Engine | Supported | Notes |
|---|---|---|
| Chrome | Yes | — |
| Firefox | Yes | — |
| Safari | Yes | — |
| Edge | Yes | — |
| Node.js | Yes | — |