-4

enter image description here

I need to parse multiple email bodies that look like using google apps script:

Name: Bob smith
Email: hellol@aol.com
Phone Number: 4243331212

I want to split on the heading at the beginning of each line .I have the following function:

function parseBody (i, body) {

   var split = body.split(new RegExp(/(\w*): ([^\t\r\n]*)[\t\r\n]*/)) //removes trailing line feed

 .filter(function (x) { return x })

 .....


 }

but the regex is not working, only capturing the first title when I test on http://www.regexpal.com/ . What am I doing wrong?

edit:

enter image description here

user1592380
  • 34,265
  • 92
  • 284
  • 515
  • @Toto, This is not a duplicate , the first question was about a syntax error. The current question is because the regex is not functioning as I expected and I don't know why. – user1592380 Aug 28 '17 at 16:51

1 Answers1

2

You need to add the g flag to make sure it doesn't stop after the first match.

/(\w*):/g

Demo

Then, your whole regex is:

/(\w*): ([^\t\r\n]*)[\t\r\n]*/g

You might want to change it to (^[^:]+) though, so you can have spaces in the first part (like in Phone Number). Also, add a m flag to make it multiline:

 /(^[^:]+): ([^\t\r\n]*)[\t\r\n]*/mg

Demo

ishegg
  • 9,685
  • 3
  • 16
  • 31
  • Thanks for looking at this. that is a step in the right direction, but if you switch to JS (https://regex101.com/r/lRxpNT/1) it gives a full match. I want to either split on the title (the part before the colon) or somehow end up with the part after the colons on each line in an array. I've added a screenshot above. – user1592380 Aug 28 '17 at 17:05
  • Is there a specific reason you need to use regex? It'd be much easier and faster to split each line on `:` and use the first and second part in the array accordingly. – ishegg Aug 28 '17 at 17:20
  • There may be some free text in the email bodies besides what I'm showing, but it might be worth a try. – user1592380 Aug 28 '17 at 17:32