我想从以下输入文字中提取电子邮件地址:
Junk
more junk
Members:
member: user1@somedomain.com (user)
member: user2@somedomain.com (user)
member: user3@somedomain.com (user)
member: user4@somedomain.com (user)
member: user5@somedomain.com (user)
member: user6@somedomain.com (user)
even more junk
地址始终在"之间找到。成员:"和" (用户)&#34 ;.以下表达式在在线正则表达式测试器中输出一组电子邮件地址:
/ member: (.*) /gi
我接受了表达式,并创建了一个匹配相应行的函数,但包括"成员:"在输出中。
function test() {
var str = 'Junk\nmore junk\nMembers:\n member: user1@somedomain.com (user)\n member: user2@somedomain.com (user)\n member: user3@somedomain.com (user)\n member: user4@somedomain.com (user)\n member: user5@somedomain.com (user)\n member: user6@somedomain.com (user)\neven more junk';
var test = str.match(/ member: (.*) /gi);
}
Output:
[ member: user1@somedomain.com , member: user2@somedomain.com , member: user3@somedomain.com , member: user4@somedomain.com , member: user5@somedomain.com , member: user6@somedomain.com ]
我是Regex的新手,但他已经尝试过积极和消极的前瞻,但似乎无法找到有效的组合。
Solution:
function regexExample() {
var myString = 'Junk\nmore junk\nMembers:\n member: user1@somedomain.com (user)\n member: user2@somedomain.com (user)\n member: user3@somedomain.com (user)\n member: user4@somedomain.com (user)\n member: user5@somedomain.com (user)\n member: user6@somedomain.com (user)\neven more junk';
var myRegexp = / member: (.*) /gi;
var match = myRegexp.exec(myString);
while (match != null) {
Logger.log (match[1]);
match = myRegexp.exec(myString);
}
}