0

I'd like to extract the email addresses from the following input text:

    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

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: 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 ]

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: 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);
      }
   }
  • The easiest way is to just use `RegExp#exec` in a loop. – Wiktor Stribiżew Jun 15 '16 at 21:43
  • @Wiktor, thanks for looking at my question. I don't think the question you linked to is the same. I can already access the groups in the output. The issue is that they include "member: " and I only want the email portion. I already have a method to iterate and strip the undesired portion, but I am looking for a more elegant regex way, which I think can be done with one expression. The correct (email only) results are generated with an online regex tester using the expression I posted above. Any ideas on how I can tune the expression to work in javascript match would be appreciated. – Vince Anderson Jun 16 '16 at 02:36
  • 1
    @Wiktor, my apologies, I didn't understand the nuance of accessing the second array, now I do. I edited the post with the solution to my specific example based on the linked question. Thx again! – Vince Anderson Jun 16 '16 at 03:12

0 Answers0