Vaibhav Agarwal Vaibhav Agarwal -4 years ago 155
Javascript Question

Javascript regex to clean email body

I have about 100 raw emails containing text similar to following:

Seems alright now. You may proceed to file the same.
Also, please update the status of TDS payment.
Thanks
---------- Forwarded message ----------
From: sender@email.com;
Date: Tue, Mar 21, 2017 at 1:14 PM
Subject: some subject
To: abc@gmail.com
Cc: xyz@ymail.com


What I need to do is to remove forwarded message part from each email and only retain the text written by sender.

Expected Output:

Seems alright now. You may proceed to file the same.
Also, please update the status of TDS payment.
Thanks


What regex pattern can I use to achieve the expected output??

Answer Source

Try that:

^(?=.*Forwarded message)[^]*

i.e in javascript:

'/^(?=.*Forwarded message)[^]*/m;'

Update

You can also try the following regex if you need to consider -:

^(?=\s*[-]+\s*Forwarded message\s*[-]+\s*)[^]*

Replace by:

"" empty

Demo

const regex = /^(?=.*Forwarded message)[^]*/m;
const str = `Seems alright now. You may proceed to file the same. 
Also, please update the status of TDS payment.
Thanks
---------- Forwarded message ---------- 
From: sender@email.com; 
Date: Tue, Mar 21, 2017 at 1:14 PM 
Subject: some subject 
To: abc@gmail.com 
Cc: xyz@ymail.com

What I need to do is to remove forwarded message part from each email and only retain the text written by sender.

Expected Output:

Seems alright now. You may proceed to file the same. 
Also, please update the status of TDS payment.
Thanks`;
const subst = ``;
const result = str.replace(regex, subst);
console.log(result);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download