Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!