Extract email addresses from a text file using JavaScript

后端 未结 7 542
没有蜡笔的小新
没有蜡笔的小新 2020-12-11 09:42

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         


        
相关标签:
7条回答
  • 2020-12-11 10:09

    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.

    0 讨论(0)
  • 2020-12-11 10:11

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

    0 讨论(0)
  • 2020-12-11 10:12

    You can try this regex instead:

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

    This should work for your case.

    0 讨论(0)
  • 2020-12-11 10:15

    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

    0 讨论(0)
  • 2020-12-11 10:19

    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 讨论(0)
  • 2020-12-11 10:21

    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
    }
    
    0 讨论(0)
提交回复
热议问题