Extracting top-level and second-level domain from a URL using regex

Here’s my idea,

Match anything that isn’t a dot, three times, from the end of the line using the $ anchor.

The last match from the end of the string should be optional to allow for .com.au or .co.nz type of domains.

Both the last and second last matches will only match 2-3 characters, so that it doesn’t confuse it with a second-level domain name.


Regex:

[^.]*\.[^.]{2,3}(?:\.[^.]{2,3})?$


Demonstration:

Regex101 Example

Leave a Comment