Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

299
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda