" "[email protected] " with one regular expression I would like" /> " "[email protected] " with one regular expression I would like" /> " "[email protected] " with one regular expression I would like"/>

Regular expression capable of extracting email from strings

44 Views Asked by At

So I have those examples:

with one regular expression I would like to be able to extract from them respectively:

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)
[
  '[email protected]',
  '[email protected]',
  undefined,
  index: 0,
  input: '[email protected]',
  groups: undefined
]
> SuzzysEmail.match(emailRegex)
[
  '[email protected] <[email protected]>',
  undefined,
  '[email protected]',
  index: 0,
  input: '[email protected] <[email protected]>',
  groups: undefined
]
> elonsEmail.match(emailRegex)
[
  'Elon Musk <[email protected]>',
  undefined,
  '[email protected]',
  index: 0,
  input: 'Elon Musk <[email protected]>',
  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.

0

There are 0 best solutions below