我有一个像这样的字符串:
Francesco Renga <[email protected] /cdn-cgi/l/email-protection>
我只需要提取电子邮件,即[电子邮件受保护] /cdn-cgi/l/email-protection.
我怎样才能在nodejs/javascript中以“优雅”的方式做到这一点?
使用正则表达式,如果您的字符串模式始终是Some text<email>
or Some text<email>, Some text<email> <email>
你可以使用这个简单的<(.*?)>
Demo https://regex101.com/r/T2qKWd/2
其他解决方案
使用正向前瞻:[^<]+(?=>)
,这是一个片段和一个demo https://regex101.com/r/T2qKWd/4/
var text = "Francesco Renga <[email protected] /cdn-cgi/l/email-protection>, Marty McFly <[email protected] /cdn-cgi/l/email-protection> Marty McFly <[email protected] /cdn-cgi/l/email-protection> <[email protected] /cdn-cgi/l/email-protection>";
var re = /[^< ]+(?=>)/g;
text.match(re).forEach(function(email) {
console.log(email);
});
解释
[^<]+
匹配除 a 之外的任何内容<
一次到无限次之间
(?=>)
随后是一个>
简单,不需要任何团体。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)