So I have those examples:
"torvalds@linux.org"
"Elon <42069@tesla.com>"
"Suzzy@Typo.com <suzzy@peppapig.com>"
with one regular expression I would like to be able to extract from them respectively:
"torvalds@linux.org"
"42069@tesla.com"
"suzzy@peppapig.com"
My miserable attempts were able me so far to came up with something like:
/^(?:([^<>]+)|(?:.*<(.*?)>))$/
but with that I'm not able to make a single group capture an email address:
> var emailRegex = /^(?:([^<>]+)|(?:.*<(.*?)>))$/;
undefined
> linus.match(emailRegex)
[
'torvalds@linux-foundation.org',
'torvalds@linux-foundation.org',
undefined,
index: 0,
input: 'torvalds@linux-foundation.org',
groups: undefined
]
> SuzzysEmail.match(emailRegex)
[
'Suzzy@PeppaTypoPig.com <suzzy@peppapig.com>',
undefined,
'suzzy@peppapig.com',
index: 0,
input: 'Suzzy@PeppaTypoPig.com <suzzy@peppapig.com>',
groups: undefined
]
> elonsEmail.match(emailRegex)
[
'Elon Musk <42069@tesla.com>',
undefined,
'42069@tesla.com',
index: 0,
input: 'Elon Musk <42069@tesla.com>',
groups: undefined
]
and I don't want to refere once to email with .match(emailRegex)[1]
and .match(emailRegex)[2]
in other cases. I'm sure there is a better way.