1

I am trying to find the perfect regex that will help me extract the domain name of an email. (ex: hello@domain.com should return domain.com)

A lot of issue on SO address this problem but none of them treat it perfectly.

Here are the special edge cases that I want to tackle:

  1. I don't want to keep subdomains. (ex: hello@sub.domain.com should return domain.com )
  2. Some domain extensions have two dots. (ex: hello@domain.co.uk should return domain.co.uk)
  3. It is possible to include an at (@) in your domain name (although most registrar do not allow it). (ex: hello@dom@in.com should return dom@in.com)

I have tried the following regex but they don't tackle all the edge cases:

  • /(?<=@)\S+\.\S+$/ => Does not handle case 1
  • /[^.@]*?\.\w{2,}$|[^.@]*?\.com?\.\w{2}$/ => Does not handle case 3
nsayer
  • 799
  • 2
  • 8
  • 21
  • *"I don't want to keep subdomains. (ex: `hello@sub.domain.com` should return `domain.com` )"* if you don't want subdomains, it should return `com` – Cid Sep 13 '21 at 16:20
  • Your case 3 would be interpreted by an email system as user "hello@dom" on domain "in.com"; multiple @ symbols are allowed in email addresses but the last one is the domain separator. https://stackoverflow.com/questions/12355858/how-many-symbol-can-be-in-an-email-address – Daniel Beck Sep 13 '21 at 16:20
  • ...actually on rereading I'm realizing it wouldn't be a valid email address at all, unless it were literally `"hello@dom"@in.com` (quotes included) – Daniel Beck Sep 13 '21 at 16:22
  • You are missing other edge cases, such as `username@123.45.67.89` – Cid Sep 13 '21 at 16:24
  • @cid, then i want want to keep the first level domain. – nsayer Sep 13 '21 at 16:25

0 Answers0