Extract all email addresses from bulk text using jquery

∥☆過路亽.° 提交于 2019-12-17 05:49:27

问题


I'm having the this text below:

sdabhikagathara@rediffmail.com, "assdsdf" <dsfassdfhsdfarkal@gmail.com>, "rodnsdfald ferdfnson" <rfernsdfson@gmail.com>, "Affdmdol Gondfgale" <gyfanamosl@gmail.com>, "truform techno" <pidfpinfg@truformdftechnoproducts.com>, "NiTsdfeSh ThIdfsKaRe" <nthfsskare@ysahoo.in>, "akasdfsh kasdfstla" <akashkatsdfsa@yahsdfsfoo.in>, "Bisdsdfamal Prakaasdsh" <bimsdaalprakash@live.com>,; "milisdfsfnd ansdfasdfnsftwar" <dfdmilifsd.ensfdfcogndfdfatia@gmail.com>

Here emails are seprated by , or ;. I want to extract all emails present above and store them in array. Is there any easy way using regex to get all emails directly?


回答1:


Here's how you can approach this:

HTML

<p id="emails"></p>

JavaScript

var text = 'sdabhikagathara@rediffmail.com, "assdsdf" <dsfassdfhsdfarkal@gmail.com>, "rodnsdfald ferdfnson" <rfernsdfson@gmal.com>, "Affdmdol Gondfgale" <gyfanamosl@gmail.com>, "truform techno" <pidfpinfg@truformdftechnoproducts.com>, "NiTsdfeSh ThIdfsKaRe" <nthfsskare@ysahoo.in>, "akasdfsh kasdfstla" <akashkatsdfsa@yahsdfsfoo.in>, "Bisdsdfamal Prakaasdsh" <bimsdaalprakash@live.com>,; "milisdfsfnd ansdfasdfnsftwar" <dfdmilifsd.ensfdfcogndfdfatia@gmail.com>';    

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

$("#emails").text(extractEmails(text).join('\n'));

Result

sdabhikagathara@rediffmail.com dsfassdfhsdfarkal@gmail.com rfernsdfson@gmal.com gyfanamosl@gmail.com pidfpinfg@truformdftechnoproducts.com nthfsskare@ysahoo.in akashkatsdfsa@yahsdfsfoo.in bimsdaalprakash@live.com dfdmilifsd.ensfdfcogndfdfatia@gmail.com

Source: Extract email from bulk text (with Regular Expressions, JavaScript & jQuery)

Demo 1 Here

Demo 2 Here using jQuery's each iterator function




回答2:


You can use this regex:

var re = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/g;

You can extract the e-mails like this:

('sdabhikagathara@rediffmail.com, "assdsdf" <dsfassdfhsdfarkal@gmail.com>, "rodnsdfald ferdfnson" <rfernsdfson@gmail.com>, "Affdmdol Gondfgale" <gyfanamosl@gmail.com>, "truform techno" <pidfpinfg@truformdftechnoproducts.com>, "NiTsdfeSh ThIdfsKaRe" <nthfsskare@ysahoo.in>, "akasdfsh kasdfstla" <akashkatsdfsa@yahsdfsfoo.in>, "Bisdsdfamal Prakaasdsh" <bimsdaalprakash@live.com>,; "milisdfsfnd ansdfasdfnsftwar" <dfdmilifsd.ensfdfcogndfdfatia@gmail.com>').match(re);

//["sdabhikagathara@rediffmail.com", "dsfassdfhsdfarkal@gmail.com", "rfernsdfson@gmail.com", "gyfanamosl@gmail.com", "pidfpinfg@truformdftechnoproducts.com", "nthfsskare@ysahoo.in", "akashkatsdfsa@yahsdfsfoo.in", "bimsdaalprakash@live.com", "dfdmilifsd.ensfdfcogndfdfatia@gmail.com"]



回答3:


Just an update to the accepted answer. This does not work for "plus" signs in the email address. GMAIL supports emailaddress+randomtext@gmail.com.

I've updated to:

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



回答4:


function GetEmailsFromString(input) {
  var ret = [];
  var email = /\"([^\"]+)\"\s+\<([^\>]+)\>/g

  var match;
  while (match = email.exec(input))
    ret.push({'name':match[1], 'email':match[2]})

  return ret;
}

var str = '"Name one" <foo@domain.com>, ..., "And so on" <andsoon@gmx.net>'
var emails = GetEmailsFromString(str)

Source




回答5:


You don't need jQuery for that; JavaScript itself supports regexes built-in.

Have a look at Regular Expression for more info on using regex with JavaScript.

Other than that, I think you'll find the exact answer to your question somewhere else on Stack Overflow - How to find out emails and names out of a string in javascript




回答6:


The bellow function is RFC2822 compliant according to Regexr.com

ES5 :

var extract = function(value) {
   var reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g;
   return value && value.match(reg);
}

ES6 :

const reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g
const extract = value => value && value.match(reg)

Regexr community source




回答7:


 const = regex = /\S+[a-z0-9]@[a-z0-9\.]+/img
"hello sean@example.com how are you? do you know bob@example.com?".match(regex)


来源:https://stackoverflow.com/questions/14440444/extract-all-email-addresses-from-bulk-text-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!