I'd like to extract the email addresses from the following input text:
Junk
more junk
Members:
member: [email protected] (user)
member: [email protected] (user)
member: [email protected] (user)
member: [email protected] (user)
member: [email protected] (user)
member: [email protected] (user)
even more junk
The addresses are consistently found between " member: " and " (user)". The following expression output an array of email addresses in an online regex tester:
/ member: (.*) /gi
I took the expression and made a function that matches the appropriate lines but includes "member: " in the output.
function test() {
var str = 'Junk\nmore junk\nMembers:\n member: [email protected] (user)\n member: [email protected] (user)\n member: [email protected] (user)\n member: [email protected] (user)\n member: [email protected] (user)\n member: [email protected] (user)\neven more junk';
var test = str.match(/ member: (.*) /gi);
}
Output:
[ member: [email protected] , member: [email protected] , member: [email protected] , member: [email protected] , member: [email protected] , member: [email protected] ]
I am fairly new to Regex but have experimented with positive and negative lookahead, but can't seem to find a combo that works.
Solution:
function regexExample() {
var myString = 'Junk\nmore junk\nMembers:\n member: [email protected] (user)\n member: [email protected] (user)\n member: [email protected] (user)\n member: [email protected] (user)\n member: [email protected] (user)\n member: [email protected] (user)\neven more junk';
var myRegexp = / member: (.*) /gi;
var match = myRegexp.exec(myString);
while (match != null) {
Logger.log (match[1]);
match = myRegexp.exec(myString);
}
}