Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

javascript - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...