Reputation: 31
For example:
var s = "'Jim,Rose'<[email protected]>, 'John'<[email protected]>, '[email protected]'<[email protected]>"
Upvotes: 2
Views: 1081
Reputation: 3722
You might try something along these lines:
var s = "'Jim,Rose'<[email protected]>, 'John'<[email protected]>, '[email protected]'<[email protected]>";
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);
Upvotes: 0
Reputation: 229
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.
Upvotes: 0
Reputation: 93684
First, I assume you have double quotes around that string:
var s = "'Jim,Rose'<[email protected]>, 'John'<[email protected]>, '[email protected]'<[email protected]>";
Then use the following regex to do a match:
var emails = s.match(/<.*?>/g);
Which produces this array:
["<[email protected]>", "<[email protected]>", "<[email protected]>"]
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'", "'[email protected]'"]
Upvotes: 1