Web
Domain Name Regex
A domain name is a series of labels separated by dots, ending in a top-level domain. Each label may contain letters, digits and dashes, must not start or end with a dash and can be at most 63 characters, and this pattern encodes those DNS rules. The top-level domain must be letters only. It works well for custom domain settings in SaaS dashboards, email domain allow-lists and DNS tooling. The honest limit is that it does not check the overall 253-character length or whether the top-level domain actually exists.
Pattern (JavaScript)
/^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}$/Starts at the start of the line/string, then (a letter or digit, then (one of the characters A-Za-z0-9-, between 0 and 61 times, then a letter or digit), optionally (zero or one time), then the character "."), one or more times, then a letter, between 2 and 63 times, then the end of the line/string.
Limit: Checks the format only; it cannot tell whether the domain is registered.
Examples
Matches
- ✓ example.com
- ✓ sub.example.org
- ✓ my-site.test
Does not match
- ✗ -example.com
- ✗ example
- ✗ example..com
Try it
0 matches (JavaScript engine)
example.com
sub.example.org
my-site.test
-example.com
example
example..comCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}$/g;
const text = "example.com";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}$')
text = 'example.com'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}$/';
$text = 'example.com';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\\.)+[A-Za-z]{2,63}$");
String text = "example.com";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}$`)
text := "example.com"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}$");
var text = "example.com";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}$").unwrap();
let text = "example.com";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}$/
text = 'example.com'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Does this accept internationalised domains?
- Only in their Punycode form, such as xn--bcher-kva.example. Convert Unicode domains to Punycode with your platform's IDN function before testing.
- Why is a single label like localhost rejected?
- The pattern requires at least one dot and a top-level domain, which is what most public-facing domain fields expect. Handle localhost or intranet names with a separate rule.
- Can a top-level domain contain digits?
- Public top-level domains are letters only, except for Punycode forms that start with xn--. If you need those, change the final class to allow digits and dashes.