最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Extract email addresses from a text file using JavaScript - Stack Overflow

programmeradmin2浏览0评论

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

    [email protected]   Active  12/05/2015  03:07
   [email protected]   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,

 [email protected]  12/05/2015  03:07
   [email protected]  8/05/2015  01:07

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

     [email protected] 
   [email protected]

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

    [email protected]   Active  12/05/2015  03:07
   [email protected]   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,

 [email protected]  12/05/2015  03:07
   [email protected]  8/05/2015  01:07

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

     [email protected] 
   [email protected]
Share Improve this question asked Oct 16, 2015 at 9:22 Vishal ChopraVishal Chopra 3465 silver badges9 bronze badges 1
  • I have written an NPM package for extracting emails from text. – Gajus Commented May 7, 2020 at 0:59
Add a ment  | 

7 Answers 7

Reset to default 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

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

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
}

Let's try with this simple regular expression:

var record = '    [email protected]   Active  12/05/2015  03:07';
var regExp = /^\s*(.*?)\s+/;
console.log(record.match(regExp)[1]);

You can try this regex instead:

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

This should work for your case.

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

Example:

var string = "    [email protected]   Active  12/05/2015  03:07   [email protected]   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.

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);
                }

                });
        });
发布评论

评论列表(0)

  1. 暂无评论