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
Thanks
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 charTRACKING NUMBER
- some fixed string (at the end of the next line).