使用jquery从批量文本中提取所有电子邮件地址

时间:2013-01-21 14:11:58

标签: javascript jquery regex text-extraction email-address

我正在阅读以下文字:

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>

此处电子邮件由,;分隔。 我想提取上面的所有电子邮件并将它们存储在数组中。有没有简单的方法使用正则表达式直接获取所有电子邮件?

8 个答案:

答案 0 :(得分:82)

以下是您如何做到这一点:

<强> 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'));

<强>结果

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

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

<强> Demo 1 Here

Demo 2 Here 使用jQuery的每个迭代器函数

答案 1 :(得分:11)

您可以使用此正则表达式:

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;

您可以像这样提取电子邮件:

('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"]

答案 2 :(得分:8)

只是对已接受答案的更新。这不适用于电子邮件地址中的“加号”标志。 GMAIL支持emailaddress+randomtext@gmail.com。

我已更新为:

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

答案 3 :(得分:3)

波纹管功能符合Regexr.com

RFC2822

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

答案 4 :(得分:2)

你不需要jQuery; JavaScript本身支持内置的正则表达式。

有关使用带有JavaScript的正则表达式的更多信息,请查看Regular Expression

除此之外,我想你会在Stack Overflow上的其他地方找到你问题的确切答案 - How to find out emails and names out of a string in javascript

答案 5 :(得分:2)

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

答案 6 :(得分:0)

 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)

答案 7 :(得分:0)

这里的一堆答案包括小写/大写字母 [a-zA-Z] 和不敏感的正则表达式标志 i,这是无稽之谈。

  • i 修饰符:不敏感。不区分大小写的匹配(忽略 [a-zA-Z] 的大小写)。
  • \d 匹配一个数字(相当于 [0-9]),因为域扩展名不以数字字符结尾。

因此,与 \d 令牌结合使用。我们得到了一个更加浓缩和优雅的句子。

/[a-z\d._+-]+@[a-z\d._-]+/gi

演示

let input = '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>'

function get_email(string) {
  return string.match(/[a-z\d._+-]+@[a-z\d._-]+/gi)
};

$('#output').html(get_email(input).join('; '));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="output"></div>