To run a regex on different posible item paths

Hello,

I am trying to extract the email address from the automatic emails saying my email was not delivered.

In order to do that, I run a regex on the string displaying the email address.
Sadly, the item path changes in some of the emails and therefore an error occurs.

var text = $item("0").$node["Webhook"].json["body"]["latest_message"]["preview"]; // The string containing
var re = /([a-zA-Z0-9._-][email protected][a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi;; // The actual regex
var email = text.match(re)

return [{
  json: { email }
}]

Most of the time, the end of the path changes to [“message”][“preview”] or [“last_message”][“preview”].
If I enter a path that doesn’t exist, the function returns an error.

Pretty sure there is a simple solution to my problem, sadly I have not been able to find it by myself.

Hi @Seven

Welcome to the Community.

Is it workable to just use regex on the complete body or part of it? So you do not have to deal with path changes?

Thanks :slight_smile:

It is not, as there are other email adresses in the body, such as the the sender of the automatic email (server) and some other email adresses relative to the mailing client I use.

Would those also change? If not they can be easily filtered out with the regex.

Some will change, such as the server adress like this one : “[email protected]
As well as the message id, for example : “[email protected]
and my email ID given by their server, for example : “[email protected]om

In the end, the easier solution would certainly be to learn the possible paths this string will have, then scan them all to know which one exists, and then run the regex on it.

Like if item1 exists, then run regex on item1, if not scan if item> exists… and so on.

You should be able to have multiple Regex possibilities with the use of the pipe in the regex.
As you have noticed the use of different fields will result in errors. You can ignore the errors and just have some checks after each other.
Or you will have to come up with a check on the whole body so you do not have to deal with errors.
Hope that makes sense.

If you would still like some help. Please provide some examples of the output you are expecting. Maybe someone will have a great idea to help you out.

Hi @Seven I’m new to this as well, but maybe this might work …

try {
    text = $item("0").$node["WebhookSampleData"].json["body"]["message"]["preview"];
} catch (e) {
    try {
      text = $item("0").$node["WebhookSampleData2"].json["body"]["latest_message"]["preview"];
    } catch (e) {
        text = "no email address found";
    }
}

instead of …

var text = $item("0").$node["WebhookSampleData"].json["body"]["message"]["preview"]; // The string containing
2 Likes

Hello @dickhoning,

Played around it a little bit to add more tries and it works !
Thanks, here is the solution in full

try {
    text = $item("0").$node["Webhook"].json["body"]["latest_message"]["preview"];
} catch (e) {
    try {
        text = $item("0").$node["Webhook"].json["body"]["message"]["preview"];
    } catch (e) {
        try {
            text = $item("0").$node["Webhook"].json["body"]["lastmessage"]["preview"];
        } catch (e) {
            try {
                text = $item("0").$node["Webhook"].json["body"]["latestmessage"]["preview"];
            } catch (e) {
                try {
                    text = $item("0").$node["Webhook"].json["body"]["last_message"]["preview"];
                } catch (e) {
                    text = "no email address found";
                }
            }
        }
    }
}
var re = /([a-zA-Z0-9._-][email protected][a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi; // The actual regex
var email = text.match(re);

return [{
    json: {
        email
    }
}];
2 Likes

@Seven you’re very welcome, and I’m glad to hear that it works!