Regex for Domain Name Validation
Quick Answer: Domain regex: ^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$. This enforces DNS label rules: 1-63 characters per label, letters/digits/hyphens only, no leading or trailing hyphens, and a valid TLD.
FAQ
What are the rules for valid domain names?
Each label (between dots) can be 1-63 chars of letters, digits, and hyphens. Cannot start or end with a hyphen. Total domain length max is 253 characters.
Does this handle internationalized domain names (IDN)?
This regex covers ASCII domains. IDN domains (e.g., example.xn--) use Punycode encoding. For IDN support, convert to Punycode first or use a dedicated IDN validation library.