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

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

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 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