cooking, programming and everyday life xrado

Thursday, February 18, 2010

Extract emails

I was doing research on possible customers and end up with the long list of names, contacts and emails. While i was thinking about how i'm gonna assemble the mailing, i came a cross the thought i'll have to collect them again and copy / paste each email to compose form. Ehhh. Then i got the idea how to pass this annoying task. A few lines of javascript and regex and i got a script that collects emails, remove duplicates, get them lower case and separated by commas. Perfect!
window.addEvent('domready',function(){
	$('button').addEvent('click',function(e){
		var emails = [];
		$('text').get('value').replace(/(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?))|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?)/ig, function(match){
			match = match.toLowerCase();
			if(emails.indexOf(match) != -1) emails.push(match);
		});
		$('text').set('value',emails.join(', '));
	});
});
DEMO  ...paste the text containing emails and press format button.
I had to try it! And it works very well, congratulations!

COMMENTS ARE DISABLED