javascriptjqueryregextext-extractionemail-address

Extract all email addresses from bulk text using jquery


I'm having the this text below:

sdabhikagathara@rediffmail.com, "assdsdf" <dsfassdfhsdfarkal@gmail.com>, "rodnsdfald ferdfnson" <rfernsdfson@gmail.com>, "Affdmdol Gondfgale" <gyfanamosl@gmail.com>, "truform techno" <pidfpinfg@truformdftechnoproducts.com>, "NiTsdfeSh ThIdfsKaRe" <nthfsskare@ysahoo.in>, "akasdfsh kasdfstla" <akashkatsdfsa@yahsdfsfoo.in>, "Bisdsdfamal Prakaasdsh" <bimsdaalprakash@live.com>,; "milisdfsfnd ansdfasdfnsftwar" <dfdmilifsd.ensfdfcogndfdfatia@gmail.com>

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?


Solution

  • Here's how you can approach this:

    HTML

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

    JavaScript

    var text = 'sdabhikagathara@rediffmail.com, "assdsdf" <dsfassdfhsdfarkal@gmail.com>, "rodnsdfald ferdfnson" <rfernsdfson@gmal.com>, "Affdmdol Gondfgale" <gyfanamosl@gmail.com>, "truform techno" <pidfpinfg@truformdftechnoproducts.com>, "NiTsdfeSh ThIdfsKaRe" <nthfsskare@ysahoo.in>, "akasdfsh kasdfstla" <akashkatsdfsa@yahsdfsfoo.in>, "Bisdsdfamal Prakaasdsh" <bimsdaalprakash@live.com>,; "milisdfsfnd ansdfasdfnsftwar" <dfdmilifsd.ensfdfcogndfdfatia@gmail.com> datum eternus hello+11@gmail.com';    
    
    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

    sdabhikagathara@rediffmail.com,dsfassdfhsdfarkal@gmail.com,rfernsdfson@gmal.com,gyfanamosl@gmail.com,pidfpinfg@truformdftechnoproducts.com,nthfsskare@ysahoo.in,akashkatsdfsa@yahsdfsfoo.in,bimsdaalprakash@live.com,dfdmilifsd.ensfdfcogndfdfatia@gmail.com,hello+11@gmail.com
    

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

    Demo 1 Here

    Demo 2 Here using jQuery's each iterator function