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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14440444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:46:21  来源:igfitidea点击:

Extract all email addresses from bulk text using jquery

javascriptjqueryregextext-extractionemail-address

提问by Milind Anantwar

I'm having the this text below:

我有下面的文字:

[email protected], "assdsdf" <[email protected]>, "rodnsdfald ferdfnson" <[email protected]>, "Affdmdol Gondfgale" <[email protected]>, "truform techno" <[email protected]>, "NiTsdfeSh ThIdfsKaRe" <[email protected]>, "akasdfsh kasdfstla" <[email protected]>, "Bisdsdfamal Prakaasdsh" <[email protected]>,; "milisdfsfnd ansdfasdfnsftwar" <[email protected]>

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?

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

回答by Leniel Maccaferri

Here's how you can approach this:

以下是您可以如何解决这个问题:

HTML

HTML

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

JavaScript

JavaScript

var text = '[email protected], "assdsdf" <[email protected]>, "rodnsdfald ferdfnson" <[email protected]>, "Affdmdol Gondfgale" <[email protected]>, "truform techno" <[email protected]>, "NiTsdfeSh ThIdfsKaRe" <[email protected]>, "akasdfsh kasdfstla" <[email protected]>, "Bisdsdfamal Prakaasdsh" <[email protected]>,; "milisdfsfnd ansdfasdfnsftwar" <[email protected]>';    

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

结果

[email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]


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

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

Demo 1 Here

演示 1 这里

Demo 2 Hereusing jQuery's each iterator function

Demo 2 这里使用 jQuery 的 each 迭代器函数

回答by Minko Gechev

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:

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

('[email protected], "assdsdf" <[email protected]>, "rodnsdfald ferdfnson" <[email protected]>, "Affdmdol Gondfgale" <[email protected]>, "truform techno" <[email protected]>, "NiTsdfeSh ThIdfsKaRe" <[email protected]>, "akasdfsh kasdfstla" <[email protected]>, "Bisdsdfamal Prakaasdsh" <[email protected]>,; "milisdfsfnd ansdfasdfnsftwar" <[email protected]>').match(re);

//["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"]

回答by Nick Caruso

Just an update to the accepted answer. This does not work for "plus" signs in the email address. GMAIL supports [email protected].

只是对已接受答案的更新。这不适用于电子邮件地址中的“加号”符号。GMAIL 支持 [email protected]

I've updated to:

我已经更新为:

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

回答by Sebastien H.

The bellow function is RFC2822compliant according to Regexr.com

波纹管的功能是RFC2822根据符合Regexr.com

ES5 :

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 :

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

Regexr 社区源

回答by Roy Dictus

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

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

Have a look at Regular Expressionfor 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 中的字符串中找出电子邮件和姓名

回答by Johan

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" <[email protected]>, ..., "And so on" <[email protected]>'
var emails = GetEmailsFromString(str)

Source

来源

回答by saeid abdi

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