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

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜