Regex library
Regular expression for domain names
A practical DNS domain regex: labels, hyphens in the middle, and a two-or-more-letter TLD. localhost fails on purpose.
^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,}$
Runs 100% in your browser. Nothing you paste is uploaded, stored, or sent to a server.
Highlighted matches
How to use
- Scan the pre-filled pattern and the sample. Hits are highlighted; misses stay plain.
- Toggle flags if you need global or case-insensitive matching. The pattern is JavaScript / ECMAScript.
- Paste your own corpus. Nothing is sent to a server.
- Read the notes below before you drop the pattern into production — several of these are intentionally incomplete.
A domain is labels joined by dots. Each label starts and ends with an alphanumeric, may contain hyphens, and is at most 63 characters. This pattern enforces that shape and requires at least one dot so localhost and bare words fail.
What this pattern will not do
Punycode (xn--) matches as ASCII; raw IDN Unicode does not.
Ports, paths, and schemes must be stripped first.
FAQ
Why is localhost rejected?
It has no dot. If you need it, add an alternation for ^(localhost)$ or treat it in code.
Are single-letter TLDs allowed?
Not here. Public TLDs are at least two characters.
Is the sample text uploaded?
No. The tester compiles the pattern with the browser’s RegExp engine and stays on the device.