Tuesday, 4 April 2017

Regex not working in NodeJs

I am using Outlook API to fetch sent emails of user. The API works fine and I am able to retrieve sent email body. Now, I want to clean the body to remove all links, headers, etc. and keep only the text written by user. Following is my regex function:

function getRegex() {

    var regex1 = /^(?=.*Forwarded message)[^]*/m;
    var regex2 = /^(?=.*From: )[^]*/m;
    var regex3 = /^(?=.*On )[^]*/m;
    var regex4 = /^(?=.*http)[^]*/m;

    return new RegExp("(" + regex1.source + ")|(" + regex2.source + ")|(" + regex3.source + ")|(" + regex4.source + ")");
}

Following is function to fetch sent emails from Outlook:

outlook.mail.getMessages({
    token: token.token.access_token,
    odataParams: queryParams,
    folderId: 'SentItems'

}, function (err, result) {

    if (err){
        console.log(err);
        return;
    }

    var mail_array = result.value;
    var outlook_sent_emails = '';

    mail_array.forEach(function (mail) {

        if (mail.BodyPreview !== '') {
            outlook_sent_emails += (mail.BodyPreview + " ");
        }
    });

    console.log(outlook_sent_emails.replace(getRegex(), ""));  //This is not working
});

The problem is this line console.log(outlook_sent_emails.replace(getRegex(), "")); show output but I am still getting all links, headers, etc. Why is this regex not working?? The same regex is working elsewhere in my code.



via Vaibhav Agarwal

No comments:

Post a Comment