Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

298
Views
Google Apps Script getPlainBody() from GmailMessage class regex not working

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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).
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!