Domain Name in JS
Match fully-qualified domain names like example.com or api.sub.example.co.uk.
Try it in the JS tester →Pattern
regexJS
(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,} (flags: g)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("(?:[a-zA-Z0-9](?:[a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,}", "g");
const input = "example.com";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
How the pattern works
Each label is 1–63 characters of letters, digits, or hyphens (not starting or ending with hyphen). One or more labels followed by a TLD of 2+ letters.
Examples
Input
example.comMatches
example.com
Input
api.sub.example.co.ukMatches
api.sub.example.co.uk
Input
not a domainNo match
—