dericcain
dericcain

Reputation: 2290

Regex for finding sub-domain in email address

I have email addresses with a subdomain and I want to get the subdomain of the email address. I have tried a few things, but they are not working. Some things include the @ which I don't need. So, basically, I want to get email@subdomain.domain.com.

Here is what I have that is including the @:

/([@])\w+/

Upvotes: 0

Views: 76

Answers (1)

heemayl
heemayl

Reputation: 41987

Assuming consistent pattern:

(?<=@)[^.]+
  • (?<=@) is zero-width positive lookbehind pattern ensuring our desired match is preceded by @

  • [^.]+ matches till the next . i.e. our desired portion

Demo

Upvotes: 3

Related Questions