T
ToolPrime

Regex for Domain Name

Validates domain names with proper label format and TLD.

Pattern

/^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/i

Live Tester

Enter a string to test

Examples

The i flag makes the pattern case-insensitive.

example.com
sub.domain.co.uk
my-site.org
-example.com
example-.com
.com

Pattern Breakdown

([a-zA-Z0-9]...) — label starts/ends with alphanumeric

\. — dot separator

+ — one or more labels

[a-zA-Z]{2,}$ — TLD of 2+ letters

Code Snippets

Javascript

/^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/i.test("example.com"); // true

Python

bool(re.match(r"^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$", "example.com", re.I))  # True

Php

preg_match('/^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/i', "example.com"); // 1

Related Patterns

Frequently Asked Questions

What is the regex for Domain Name?
The regex pattern for Domain Name is /^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/i. Validates domain names with proper label format and TLD.
How do I use the Domain Name regex in JavaScript?
In JavaScript you create the pattern with a regex literal or the RegExp constructor and call .test() to check a string against it. A ready-to-copy example is shown in the Code Snippets section above, alongside Python and PHP versions.
What does the Domain Name pattern match?
It matches strings like example.com; it rejects strings like -example.com. See the Examples section above for the full list of matching and non-matching cases.

Related Tools