0

I get automated emails from another system (Bananatag) and am trying to create a Google Apps Script (GAS) to extract specific information from the email. I have started creating the following code:

function fieldExtract(){
  var threads = GmailApp.search('from:noreply@bananatag.com');
  for(var m in threads){
  var messages = threads[m].getMessages(),
      tid = threads[m].getId();
  for(var b in messages){
      var body = messages[b].getPlainBody(),
          subj = messages[b].getSubject();
      var CountryExpression = new RegExp("[\n\r].*Country:\s(.*)","g");
      var RegionExpression = new RegExp("[\n\r].*Region:\s(.*)","g"),
          CityExpression = new RegExp("[\n\r].*City:\s(.*)","g"),
          OSExpression = new RegExp("[\n\r].*OS:\s(.*)","g"),
          DeviceExpression = new RegExp("[\n\r].*Device:\s(.*)","g"),
          ClientExpression = new RegExp("[\n\r].*Client:\s(.*)","g"),
          subjExpression = new RegExp("'\s*(.*?)\s*'","g");
      var subject = subjExpression.exec(subj);
      var country = CountryExpression.exec(body)[1];
    }
  }
}

The email body contains information like this:

Email Open Notification

-----------------------------------------------
EMAIL DETAILS
-----------------------------------------------
Subject: Re: FW: Payment confirmation: Invoice 1540
Sent at: Tue 17 Jan 2017 03:27:27 PM MST

RECIPIENTS:

Name: {name}
Email: {email}
-----------------------------------------------


View full details at:
https://member.bananatag.com/tag/xxxxxxx

-----------------------------------------------
OPEN DETAILS
-----------------------------------------------
LOCATION:
Country: United States
Region: WA
City: Unknown

ADDITIONAL INFORMATION:
OS: Windows 8
Device: Desktop
Browser/Client: Outlook 2010
Ok

When I test my RegEx on some testing sites like regex101, it correctly finds the text that I want. But, when I run it in GAS it throws this error:

TypeError: Cannot read property "1" from null. (line 44, file "Email-management")

Line 44 is this line var country = CountryExpression.exec(body)[1];.

If I leave off the [1] it sets country to null. Why would GAS fail to find the matching text when it definitely exists?

How do I make this work?

UPDATE

I changed the code to this:

var CountryExpression = /[\n\r].*Country:\s(.*)/g; 

and it works now

davids
  • 5,397
  • 12
  • 57
  • 94
  • 1
    All your regexps must be converted from `new RegExp("[\n\r].*Device:\s(.*)","g")` to `/[\n\r].*Device:\s(.*)/g` notation. – Wiktor Stribiżew Feb 01 '17 at 19:51
  • The linked question is not the same question and the provided answer does not match your comment answer. Please explain more. – davids Feb 01 '17 at 19:58
  • `/\b/` = `new RegExp("\\b")` – Wiktor Stribiżew Feb 01 '17 at 20:00
  • While I now understand that those are the same, someone else searching this will not know those are the same and so will not find the answer they need. You first comment helped me fix the issue and it now works. Thanks! – davids Feb 01 '17 at 20:05
  • I do think the question should be reopened, then marked as `closed` instead of `duplicate` because it isn't an exact duplicate and just because a question has the same answer doesn't mean the question is the same. i.e. 2x2=4 and 3860660/965165=4 but they are definitely not the same question. – davids Feb 01 '17 at 20:50
  • There are tons of the same questions where the only answer is *double backslashes or use regex literal notation*. Backslashes in JS are also explained very well. [Here](http://stackoverflow.com/questions/10769964/backslashes-regular-expression-javascript), [there](http://stackoverflow.com/questions/6521572/javascript-regex-not-working), and [here](http://stackoverflow.com/questions/4914484/regex-created-via-new-regexpmystring-not-working-backslashes), and [here](http://stackoverflow.com/questions/6149770/what-is-the-difference-between-using-regexp-and-using-forward-slash-notation-to). – Wiktor Stribiżew Feb 01 '17 at 20:55
  • Thank you for proving my point. They are all different questions, with the same answer. When people, like me, have limited RegEx experience, they don't know how to ask the question or how to identify the issue. Even if I had found those other articles, I wouldn't have recognized that they are different versions of the same problem. If you hadn't provided the first response, I wouldn't have know how to fix my issue. – davids Feb 01 '17 at 22:00

0 Answers0