\". For example:" />
开发者

JavaScript Regular Expressions to get all e-mails from a string

开发者 https://www.devze.com 2023-03-10 20:13 出处:网络
Given a string, how can I 开发者_运维百科match all e-mails thar are inside \"< >\". For example:

Given a string, how can I 开发者_运维百科match all e-mails thar are inside "< >".

For example:

I can have xxx@abc.com and <yyy@abc.com> and I only want to match the yyy@abc.com.

Thanks!


To be really thorough you could implement a regex from RFC822, which describes valid email address formats, however, you could save time and headache by doing something quick and simple like this:

var extractEmailAddress = function(s) {
  var r=/<?(\S+@[^\s>]+)>?/, m=(""+s).match(r);
  return (m) ? m[1] : undefined;
};
extractEmailAddress('xxx@abc.com'); // => "xxx@abc.com"
extractEmailAddress('<yyy@abc.com>'); // => "yyy@abc.com"

Of course, this function will be very permissive of strings that might conceivably even remotely look like an email address, so the regular expression "r" could be improved if quality is a concern.

0

精彩评论

暂无评论...
验证码 换一张
取 消