amponvizhi
amponvizhi

Reputation: 31

Using javascript, how can i split list of email ids that has comma or "@"even in display name?

For example:

var s = "'Jim,Rose'<[email protected]>, 'John'<[email protected]>, '[email protected]'<[email protected]>"

Upvotes: 2

Views: 1081

Answers (3)

Jonathan Camenisch
Jonathan Camenisch

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

sbrian
sbrian

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

David Tang
David Tang

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

Related Questions