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

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>

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>

I would like to add to @Ambrish Pathak's answer,

According to wikipedia, an email address can also accept + sign

([a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)

will work like a charm


[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_.]+ worked for me, you can check the result on this regex101 saved regex.

It's really just twice the same pattern separated by an @ sign.

The pattern is 1 or more occurences of:

  • a-z: any lowercase letter
  • A-Z: any uppercase letter
  • 0-9: any digit
  • -_.: a hyphen, an underscore or a dot

If it missed some emails, add any missing character to it and it should do the trick.

Edit

I didn't notice it first, but when going to the regex101 link, there's an Explanation section at the top-right corner of the screen explaining what the regular expression matches.


You can use the following regex to capture all the email addresses.

(?<name>[\w.]+)\@(?<domain>\w+\.\w+)(\.\w+)?

see demo / explanation

additionally if you want, you can capture only those emails that contains a specific domain name (ie. some-url.com) and to achieve that you just need to replace the \w+\.\w+ part after <domain> with your desired domain name. so, it would be like (?<name>[\w.]+)\@(?<domain>outlook.com)(\.\w+)?

see demo / explanation


\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}+\.[A-Z]{2,}