Search code examples
javascriptregex

regex extract email from strings


I want to know if by using regular expressions I am able to extract emails from the following strings?

The following RE pattern is .*@.*match with all strings. It has worked fine with some of the string, though with not all.

I want to match all strings match with email pattern include all domain like (some-url.com) or (some-url.co.id)

boleh di kirim ke email saya [email protected] tks...
boleh minta kirim ke [email protected]. 
[email protected]. .
[email protected] Senior Quantity Surveyor
[email protected], terimakasih bu Cindy Hartanto
[email protected] saya mau dong bu cindy
[email protected] 
Hi Cindy ...pls share the Salary guide to [email protected] thank a

Solution

  • You can create a function with regex /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/ to extract email ids from long text

    function extractEmails (text) {
      return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
    }
    

    Script in action: Run to see result

    var text = `boleh di kirim ke email saya [email protected] tks... boleh minta kirim ke [email protected]. [email protected]. . 
    [email protected] Senior Quantity Surveyor
    [email protected], terimakasih bu Cindy Hartanto
    [email protected] saya mau dong bu cindy
    [email protected] 
    Hi Cindy ...pls share the Salary guide to [email protected] thank a`; 
    
    function extractEmails ( text ){
        return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
        }
         
        $("#emails").text(extractEmails(text));
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <p id="emails"></p>

    -----Update-----

    While the regex in the above code snippet matches most email patterns, but if you still need to match >99% of the email patterns, including the edge cases (like '+' in the email) then use the regex pattern as shown below

    Script in action: Run to see result

    var text = `boleh di kirim ke email saya [email protected] tks... boleh minta kirim ke [email protected]. [email protected]. . 
    [email protected] Senior Quantity Surveyor
    [email protected], terimakasih bu Cindy Hartanto
    [email protected] saya mau dong bu cindy
    [email protected] 
    Hi Cindy ...pls share the Salary guide to [email protected] thank a`; 
    
    function extractEmails ( text ){
        return text.match(/(?:[a-z0-9+!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/gi);
        }
         
        $("#emails").text(extractEmails(text));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
    <p id="emails"></p>