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 st
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
Hope this will work.
[\w\.]+\@[\w]+(?:\.[\w]{3}|\.[\w]{2}\.[\w]{2})\b
Regex Demo
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 ekoprasetyo.crb@outlook.com tks... boleh minta kirim ke db.maulana@gmail.com. dee.wien@yahoo.com. .
deninainggolan@yahoo.co.id Senior Quantity Surveyor
Fajar.rohita@hotmail.com, terimakasih bu Cindy Hartanto
firmansyah1404@gmail.com saya mau dong bu cindy
fransiscajw@gmail.com
Hi Cindy ...pls share the Salary guide to donny_tri_wardono@yahoo.co.id 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 then use the regex pattern as shown below
Script in action: Run to see result
var text = `boleh di kirim ke email saya ekoprasetyo.crb@outlook.com tks... boleh minta kirim ke db.maulana@gmail.com. dee.wien@yahoo.com. .
deninainggolan@yahoo.co.id Senior Quantity Surveyor
Fajar.rohita@hotmail.com, terimakasih bu Cindy Hartanto
firmansyah1404@gmail.com saya mau dong bu cindy
fransiscajw@gmail.com
Hi Cindy ...pls share the Salary guide to donny_tri_wardono@yahoo.co.id 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>
[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 letterA-Z
: any uppercase letter0-9
: any digit-_.
: a hyphen, an underscore or a dotIf 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.
Hi this regex is not working for me ......
let items1 = document.querySelectorAll('h1');
items1.forEach((item) => {
let pattern = (/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi)
if (item.innerHTML.match(pattern)) {
results.push({
emailId: item.innerHTML
});
}
});
This is the tag from which i am getting the text...Just a eg.
<h1>"dasd xyz@gmail.com asdas asdsad asdasd"</h1>
This is the object output that I am getting.
[
{ emailId: 'sadasdsa br@gmail.com' },
{ emailId: 'sadasd br2@gmail.com' },
{ emailId: '"asgdb@gmail.com"' },
{ emailId: '"dasd xyz@gmail.com asdas"' },
{ emailId: 'asgdb@gmail.com' }
]
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