3

I have more than 2000 email addresses. which i have exported from feedburner. And the email address look like below;

    adminvicky@gmail.com   Active  12/05/2015  03:07
   adminvishal250@gmail.com   Pending Verification 8/05/2015  01:07

I want to extract email address from the text file by removing Active, Pending Verification, Date [i.e. 8/05/2015] and time [i.e 03:07] using JavaScript.

I have created a JavaScript Program which something like below which working properly for removing Active, Pending verification text,

<script>
  function extracter() {

    var a = document.getElementById('input').value;

    document.getElementById('output').innerHTML =

    a.replace(/Active|Pending|Verification| /g, '');

  }
</script>

<textarea id="input"></textarea><br/>
<br/>

<input type="button" value="click" onclick="extracter()"/>
<br/>
<br/>
<textarea id="output"></textarea>

And the output is,

 adminvicky@gmail.com  12/05/2015  03:07
   adminvishal250@gmail.com  8/05/2015  01:07

And I want the below output. Just help me to remove "Date" and "Time",

     adminvicky@gmail.com 
   adminvishal250@gmail.com
Vishal Chopra
  • 364
  • 5
  • 9
  • I have written an [NPM package](https://github.com/gajus/extract-email-address) for extracting emails from text. – Gajus May 07 '20 at 00:59

7 Answers7

4

Try this one, i think it will do the job

var a = document.getElementById('input').value;

document.getElementById('output').innerHTML = extractEmails(a).join('\n');

And the function:

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

Here is a fiddle

Here is also an example using jQuery also Extract all email addresses from bulk text using jquery

Community
  • 1
  • 1
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
1

I would use string.split(" ") and split the textfile at its spaces.

Example:

var string = "    adminvicky@gmail.com   Active  12/05/2015  03:07   adminvishal250@gmail.com   Pending Verification 8/05/2015  01:07"
var array = string.split(" ");
var emails = [];
for(var i = 0; i < array.length; i++){
    if(array[i].indexOf("@") != -1){
       emails.push(array[i]);
    }
};

Then you have an array emails which contains your email adresses.

Danmoreng
  • 2,367
  • 1
  • 19
  • 32
1

Try to use this regex:

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

REGEX DEMO

In your Javascript you can implement it like this:

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

JSFIDDLE DEMO

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

you can easily write a regex and iterate over the results like:

var reg = new RegExp(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g);   
var email;
while((email = reg.exec(targetText)) !== null) {
    // do something with the email
}
Safari
  • 3,302
  • 9
  • 45
  • 64
1

Let's try with this simple regular expression:

var record = '    adminvicky@gmail.com   Active  12/05/2015  03:07';
var regExp = /^\s*(.*?)\s+/;
console.log(record.match(regExp)[1]);
Andrea
  • 3,370
  • 1
  • 17
  • 25
1

You can try this regex instead:

a.replace(/\s+.+$/g, '')

This should work for your case.

Samuel Imolorhe
  • 664
  • 4
  • 10
0

Using JQuery load function to read content from .txt file and display email as hyperlink:

$(document).ready(function(){
        //Get the text content from txt file using load function
            $( "#divid" ).load( "/xyz.txt",function(response, status, xhr){
                if(status=='success') {
                     /* After loading the static text, modifying the email address to hyper link */
                      var corrected = response;
                      var emailRegex =/[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/g;
                      corrected.match(emailRegex).forEach(function(email) {
                      console.log(email);
                      corrected = corrected.replace ( email, '<a href="mailto:' + email + '">' + email + '</a>' );
                    });

                    $('#divid').html(corrected);
                }

                });
        });