HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
1. Feel Free to ask and submit anything on and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.

javascript-regex: How to extract email id from a large text?

I want to extract only the email ids from a large text. Please give me some sample code to extract it.

javascript x 33
regex x 7
Posted On : 2014-04-17 22:18:31.0
profile Garima Gupta - Garima Gupta


To extract the email ids from a large text we should use regular expression(i.e. regex) to find the email ids in a large text. This task can be done easily by using javascript and html only. First understand the scenario to perform this task. First of all we take a text area to paste the large text containing email ids. and now we get the whole text in a variable by javascript and match the regex pattern of email. and as we find any text matching the email pattern store it in an array. Thus we traverse whole text and all email ids will be added to an array one by one. Following is the simple html code as GUI to take the large text as input.


<FORM name="extractor">
<TABLE bgcolor="#CCCCCC" cellpadding=1 cellspacing=0 border=0>
<TABLE border=0 cellpadding=8 cellspacing=0 bordercolor="#000000" bgcolor="#CCCCCC">
<TR class="titlebarcolor">
<TD valign="TOP" colspan=2 ><h2 align="center"><FONT class="titlefont"><strong>Email
<TD valign="TOP" align="CENTER" colspan=2>
var introtext = 'Copy text from any source and paste it into here. Then click extract button. You can select different separator (or enter your own), group a number of emails and sort extracted emails alphabetically.';
document.write('<TEXTAREA NAME="rawdata" rows=12 cols=50 onFocus="if (this.value == introtext) this.value = \'\';">' + introtext + '</TEXTAREA>');
<TD valign="TOP" align="LEFT" colspan=2> Separator:
<SELECT name="sep">
<OPTION value=", ">Comma</OPTION>
<OPTION value="|">Pipe</OPTION>
<OPTION value=" : ">Colon</OPTION>
<OPTION value="new">New Line</OPTION>
<OPTION value="other">Other</OPTION>
<INPUT type="TEXT" name="othersep" size=3 onBlur="checksep(this.value)">
<INPUT type="TEXT" size=3 name="groupby" onBlur="numonly(this.value)">
<LABEL for="sortbox">
<INPUT type="CHECKBOX" name="sort" id="sortbox">
Sort Alphabetically</LABEL>
<TR valign="TOP">
<TD align="LEFT">
<INPUT name="BUTTON" type="BUTTON" class="button" onClick="findEmail()" value="Extract">
<INPUT name="RESET" type="RESET" class="button" value="Reset">
<SCRIPT language="JavaScript" type="text/javascript">

<TD align="RIGHT" valign="BOTTOM" nowrap> Email count:
<INPUT name="count" size=5 readonly>

As we can see the above html code is simple a html form in which we have some elements like:
1. A text area: To take the input large text from user.
2. A select menu: to select the separator to separate the email ids from each other found in large text.
3. A text field: to take the other symbols as a separator not in select menu.
4. A text field: named groupby to accept the integer value to make the group of input integer value of emails separated by new line.
5. A checkbox: to detect whether you want to sort the emails alphabetically or not.
6. A Button Labeled Extract: to extract the emails
7. A Button Labeled Reset: to reset the inputs.
8. A text field: to display the total no. of emails found in large text.

Now Let's focus on javascript code to be pasted either in body tag or in head tag of your html page.

<script type="text/javascript">
function findEmail() {
var email = "No email address detected";
var a = 0;
var ingroup = 0;
var separator = document.extractor.sep.value;
var groupby = Math.round(document.extractor.groupby.value);

if (separator == "new") separator = "\n";
if (separator == "other") separator = document.extractor.othersep.value;
var rawemail = document.extractor.rawdata.value.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi); //match the email pattern in large text pasted in textarea.
var norepeat = new Array();
if (rawemail) {
for (var i=0; i<rawemail.length; i++) {
var repeat = 0;

// Check for repeated emails routine
for (var j=i+1; j<rawemail.length; j++) {
if (rawemail[i] == rawemail[j]) {

// Create new array for non-repeated emails
if (repeat == 0) {
norepeat[a] = rawemail[i];
if (document.extractor.sort.checked) norepeat = norepeat.sort(); // Sort the array
email = "";
// Join emails together with separator
for (var k = 0; k < norepeat.length; k++) {
if (ingroup != 0) email += separator;
email += norepeat[k];

// Group emails if a number is specified in form. Each group will be separate by new line.
if (groupby) {
if (ingroup == groupby) {
email += '\n\n';
ingroup = 0;

// Return array length
var count = norepeat.length;

// Print results
document.extractor.count.value = count;
document.extractor.rawdata.value = email;

Now Let's see the result of above code:
Click here to see the output

Posted On : 2014-04-17 23:38:53
Satisfied : 1 Yes  1 No
profile Saksham Kumar - Saksham Kumar
Reply This Thread

Post Answer
Please Login First to Post Answer: Login login with facebook -