Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

300
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda