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

173
Visualizações
javascript regex data until new line

I'm trying to remove all sentences if they start with particular character until break line. So far I created something like this but it removes only these particular characters instead of whole line. I'm not good with regex expressions so would be grateful if someone could throw some hint


From: ABC, DEF <abc.l@def.com [mailto:abc.l@def.com]>

Upcoming Events:

Hello hello

Sent: Thursday, February 28, 20117:22 PM


var cleanup = msg.replace(/From.|Sent.|To.|Cc.*\n/g, "");

current output:

ABC, DEF <abc.l@def.com [mailto:abc.l@def.com]>

Upcoming Out of Office:

Hello hello

Thursday, July 29, 2021 7:48 PM


expected output:

Upcoming Out of Office:
Hello hello

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use

text = text.replace(/^\s*(?:From|Sent|To|Cc)\b.*[\r\n]*\s*/gm)

Or, if you need to also remove empty lines:

text = text.replace(/^(?:\s*(?:From|Sent|To|Cc)\b.*)?[\r\n]*\s*/gm)

See the regex demo #1 and regex demo #2. Details:

  • ^ - start of a line (m flag allows that)
  • \s* - zero or more whitespaces
  • (?:From|Sent|To|Cc) - any of the alternatives
  • \b - a word boundary
  • .* - the rest of the line
  • [\r\n]* - zero or more CR or/and LF chars
  • \s* - zero or more whitespaces.

In the second regex, the \s*(?:From|Sent|To|Cc)\b.* part is made optional so that any libe breaks right after the beginning of a line could get consumed.

JavaScript demo:

const text = "From: ABC, DEF <abc.l@def.com [mailto:abc.l@def.com]>\n\nUpcoming Events:\n\nHello hello\n\nSent: Thursday, February 28, 20117:22 PM";
const rx = /^(?:\s*(?:From|Sent|To|Cc)\b.*)?[\r\n]*\s*/gm;
console.log(text.replace(rx, ""));

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