2

this is my first question on stackoverflow so plz let me know how I can improve readability for others.

Am trying to use regex on the string I obtained from getPlainBody() in GmailMessage Class but somehow it doesn't work when I try to do it directly on the string returned by getPlainBody() but works well when I manually add \n characters.

Code that works:

function RegularExp() {
  
  //manually entered \n characters into string that I copied and pasted from getPlainBody()

  var string = "Personal Message\nraw material: oak wood 100kg\nTRACKING NUMBER 7777777777\n<somehyperlink\nFROM SomeBrand"; 

  //my goal is to get: raw material: oak wood 100kg

  var regExp = new RegExp("(.*?)\n(?=TRACKING NUMBER)","g"); 

  var PersonalMessage = regExp.exec(string)[1];
  Logger.log(PersonalMessage); //works perfectly fine

}

Code that doesn't work:

for (var j in messages){
  var message = messages[j];
  var plainText = message.getPlainBody(); //getting plainbody of fedex mail of interest

  //trying to extract the personal message 
  var regExp = new RegExp("(.*?)\n(?=TRACKING NUMBER)","g");  
  var PersonalMessage = regExp.exec(plainText)[1];
  Logger.log(PersonalMessage); //won't show anything
}

My question is why does it work when I manually enter \n but not when I use the string that was returned from getPlainBody()? I'm using the exact same regex pattern and can't see why.

Below are the links I used to try to solve my problem (or I might just be dumb not being able to apply the solution to this issue)

Newline in gmail app script getplainbody function

Google Apps Script: getPlainBody() weird behavior

Regex - google apps script

Thanks

6
  • 1
    Try this: (.*?)\\n(?=TRACKING NUMBER) Commented Aug 22, 2021 at 17:47
  • @Alireza I tried that with and without the g flag but still didn't work :( Commented Aug 22, 2021 at 17:48
  • 1
    Try these: ([^\\n]+?)(?=\\nTRACKING NUMBER) or ([^\n]+?)(?=\nTRACKING NUMBER) hope it work for you. Commented Aug 22, 2021 at 17:52
  • @Alireza what... are you MAGICIAN?! ..?! works like charm.. i used my first regex code in regexr.com and other websites - worked fine but somehow it didn't work in google apps script. may i ask you how you solved this problem? regex is so useful but definitely a challenge. Commented Aug 22, 2021 at 17:57
  • I will write an answer and explanation for it. Commented Aug 22, 2021 at 18:07

1 Answer 1

2

The issue is that the . does not match a CR char in the JavaScript regex (ECMAScript flavor).

You can use

var regExp = /(.*)(?=\r?\nTRACKING NUMBER)/g; 

The regex matches

  • (.*) - Group 1: any zero or more chars other than line break chars (it does not match LF and CR chars)
  • (?=\r?\nTRACKING NUMBER) - a positive lookahead that matches a location that is immediately followed with
    • \r? - an optional CR (carriage return char)
    • \n - a line feed char
    • TRACKING NUMBER - some fixed string (at the end of the next line).
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.