开发者

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

开发者 https://www.devze.com 2023-02-09 00:57 出处:网络
For example: var s = \"\'Jim,Rose\'<jim@l.com>, \'John\'<john@p.com>, \'jack@k.com\'<jack@k.com>\"开发者_StackOverflow

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);
0

精彩评论

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