Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract all email addresses from bulk text using jquery

I'm having the this text below:

[email protected], "assdsdf" <[email protected]>, "rodnsdfald ferdfnson" <[email protected]>, "Affdmdol Gondfgale" <[email protected]>, "truform techno" <[email protected]>, "NiTsdfeSh ThIdfsKaRe" <[email protected]>, "akasdfsh kasdfstla" <[email protected]>, "Bisdsdfamal Prakaasdsh" <[email protected]>,; "milisdfsfnd ansdfasdfnsftwar" <[email protected]>

Here emails are seprated by , or ;. I want to extract all emails present above and store them in array. Is there any easy way using regex to get all emails directly?

like image 889
Milind Anantwar Avatar asked Jan 21 '13 14:01

Milind Anantwar


4 Answers

Here's how you can approach this:

HTML

<p id="emails"></p>

JavaScript

var text = '[email protected], "assdsdf" <[email protected]>, "rodnsdfald ferdfnson" <[email protected]>, "Affdmdol Gondfgale" <[email protected]>, "truform techno" <[email protected]>, "NiTsdfeSh ThIdfsKaRe" <[email protected]>, "akasdfsh kasdfstla" <[email protected]>, "Bisdsdfamal Prakaasdsh" <[email protected]>,; "milisdfsfnd ansdfasdfnsftwar" <[email protected]> datum eternus [email protected]';    

function extractEmails (text)
{
    return text.match(/([a-zA-Z0-9._+-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
}
     
$("#emails").text(extractEmails(text).join('\n'));

Result

[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected]

Source: Extract email from bulk text (with Regular Expressions, JavaScript & jQuery)

Demo 1 Here

Demo 2 Here using jQuery's each iterator function

like image 141
Leniel Maccaferri Avatar answered Nov 15 '22 22:11

Leniel Maccaferri


You can use this regex:

var re = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/g;

You can extract the e-mails like this:

('[email protected], "assdsdf" <[email protected]>, "rodnsdfald ferdfnson" <[email protected]>, "Affdmdol Gondfgale" <[email protected]>, "truform techno" <[email protected]>, "NiTsdfeSh ThIdfsKaRe" <[email protected]>, "akasdfsh kasdfstla" <[email protected]>, "Bisdsdfamal Prakaasdsh" <[email protected]>,; "milisdfsfnd ansdfasdfnsftwar" <[email protected]>').match(re);

//["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"]
like image 33
Minko Gechev Avatar answered Nov 15 '22 21:11

Minko Gechev


Just an update to the accepted answer. This does not work for "plus" signs in the email address. GMAIL supports [email protected].

I've updated to:

return text.match(/([a-zA-Z0-9._+-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
like image 13
Nick Caruso Avatar answered Nov 15 '22 20:11

Nick Caruso


The bellow function is RFC2822 compliant according to Regexr.com

ES5 :

var extract = function(value) {
   var reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g;
   return value && value.match(reg);
}

ES6 :

const reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g
const extract = value => value && value.match(reg)

Regexr community source

like image 3
Sebastien H. Avatar answered Nov 15 '22 22:11

Sebastien H.