[英]Extract email address from string
I have a string like this:我有一个这样的字符串:
Francesco Renga <francesco_renga-001@gmail.com>
I need to extract only the email, ie francesco_renga-001@gmail.com.我只需要提取 email,即 francesco_renga-001@gmail.com。
How can I do this in nodejs/javascript in "elegant" way?如何以“优雅”的方式在 nodejs/javascript 中执行此操作?
Using regex, if your string pattern is always Some text<email>
or Some text<email>, Some text<email> <email>
you can use this simple one <(.*?)>
使用正则表达式,如果您的字符串模式始终为
Some text<email>
或Some text<email>, Some text<email> <email>
,则可以使用此简单的<(.*?)>
Other solution 其他解决方案
Use positive lookahead : [^<]+(?=>)
, here is a snippet and a demo 使用正向前瞻:
[^<]+(?=>)
,这是一个代码段和一个演示
var text = "Francesco Renga <francesco_renga-001@gmail.com>, Marty McFly <mmcfly@gmail.com> Marty McFly <mmcfly@gmail.com> <mmcfly2@gmail.com>"; var re = /[^< ]+(?=>)/g; text.match(re).forEach(function(email) { console.log(email); });
Explanation 说明
[^<]+
match anything but a <
between one and unlimited times [^<]+
匹配除<
无限制次数之间的任何内容
(?=>)
followed by a >
(?=>)
后跟一个>
Simple and does not require any group. 简单,不需要任何组。
Here's a simple example showing how to use regex in JavaScript : 这是一个简单的示例,显示了如何在JavaScript中使用正则表达式:
var string = "Francesco Renga <francesco_renga-001@gmail.com>"; // Your string containing var regex = /<(.*)>/g; // The actual regex var matches = regex.exec(string); console.log(matches[1]);
Here's the decomposition of the regex /<(.*)>/
: 这是正则表达式
/<(.*)>/
的分解:
/
and /
are mandatory to define a regex /
和/
是定义正则表达式所必需的 <
and >
simply matches the two <
and >
in your string <
和>
只需匹配字符串中的两个<
和>
()
parenthesis "capture" what you're looking for. ()
括号“捕获”您要查找的内容。 Here, they get the mail address inside. .*
: .
.*
: .
means "any character", and *
means "any number of times. Combined, it means "any character any number of times", and that is inside <
and >
, which correspond to the place where the mail is. *
表示“任何次数。结合起来,表示”任何次数的任何字符”,并且在<
和>
,与邮件所在的位置相对应。 Here's a simple code showing how extract the unique list of emails address using JavaScript:这是一个简单的代码,展示了如何使用 JavaScript 提取电子邮件地址的唯一列表:
let emaillst = string .match(/([a-zA-Z0-9._+-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
if (emaillst === null) {
// no Email Address Found
} else {
const uniqueEmail = Array.from(new Set(emaillst));
const finaluniqueEmail = [];
for(let i=0; i<=uniqueEmail.length; i++){
let characterIs = String(uniqueEmail[i]).charAt(String(uniqueEmail[i]).length - 1)
if(characterIs==='.'){
finaluniqueEmail.push(String(uniqueEmail[i].slice(0, -1)))
}else{
finaluniqueEmail.push(uniqueEmail[i]);
}
}
emaillst = finaluniqueEmail.join('\n').toLowerCase();
console.log(matches[1]);
See the Live Demo of email address extractor online 在线查看 email 地址提取器的现场演示
Features特征
for(let i=0; i<=uniqueEmail.length; i++){
let characterIs = String(uniqueEmail[i]).charAt(String(uniqueEmail[i]).length - 1)
if(characterIs==='.'){
finaluniqueEmail.push(String(uniqueEmail[i].slice(0, -1)))
I don't understand why you need this block of code.我不明白你为什么需要这段代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.