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.
精彩评论