For example:
var s = "'Jim,Rose'<jim@l.com>, 'John'<john@p.com>, 'jack@k.com'<jack@k.com>"开发者_StackOverflow
First, I assume you have double quotes around that string:
var s = "'Jim,Rose'<jim@l.com>, 'John'<john@p.com>, 'jack@k.com'<jack@k.com>";
Then use the following regex to do a match:
var emails = s.match(/<.*?>/g);
Which produces this array:
["<jim@l.com>", "<john@p.com>", "<jack@k.com>"]
With a bit of trickery, you can get rid of the <
and >
as well:
emails = s.match(/<.*?>/g).join(',').replace(/<|>/g, '').split(',');
If in fact you wanted the "display names" instead, you can do a very similar operation with:
var names = s.match(/'.*?'/g);
Which would produce this array instead:
["'Jim,Rose'", "'John'", "'jack@k.com'"]
Google's Closure library has the best solution for this I've seen. See http://www.sbrian.com/2011/01/javascript-email-address-validation.html. It's really easy to integrate the needed files.
You might try something along these lines:
var s = "'Jim,Rose'<jim@l.com>, 'John'<john@p.com>, 'jack@k.com'<jack@k.com>";
matches = s.match(/'[^']+' *<[^>]+>/g);
This should work if all names are single-quoted. It's more complicated if you need to catch something like "Mike O'Niel" . To allow for that:
matches = s.match(/(['"]).+?\1 *<[^>]+>/g);
精彩评论