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

时间:2022-10-23 19:19:06

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?

这里的电子邮件是由,或;。我想提取上面所有的邮件,并将它们存储在数组中。使用regex直接获取所有电子邮件有什么简单的方法吗?

5 个解决方案

#1


64  

Here's how you can approach this:

你可以这样做:

HTML

HTML

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

JavaScript

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)

源:从大文本中提取电子邮件(使用正则表达式、JavaScript和jQuery)

Demo 1 Here

演示1

Demo 2 Here using jQuery's each iterator function

演示2使用jQuery的每个迭代器函数

#2


8  

You can use this regex:

您可以使用这个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


5  

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

只是对已接受的答案的更新。这不适用于电子邮件地址中的“+”符号。GMAIL支持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


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

#5


2  

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

不需要jQuery;JavaScript本身支持内置的regexes。

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

查看正则表达式,了解更多关于使用正则表达式和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

除此之外,我认为您可以在Stack Overflow上找到您的问题的确切答案——如何从javascript字符串中查找电子邮件和名称

#1


64  

Here's how you can approach this:

你可以这样做:

HTML

HTML

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

JavaScript

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)

源:从大文本中提取电子邮件(使用正则表达式、JavaScript和jQuery)

Demo 1 Here

演示1

Demo 2 Here using jQuery's each iterator function

演示2使用jQuery的每个迭代器函数

#2


8  

You can use this regex:

您可以使用这个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


5  

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

只是对已接受的答案的更新。这不适用于电子邮件地址中的“+”符号。GMAIL支持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


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

#5


2  

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

不需要jQuery;JavaScript本身支持内置的regexes。

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

查看正则表达式,了解更多关于使用正则表达式和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

除此之外,我认为您可以在Stack Overflow上找到您的问题的确切答案——如何从javascript字符串中查找电子邮件和名称