Extract URL from email

i'm using node-red-node-email to read email.

What I want is to extract a URL from the email body, and send only that onwards, how can I do this?

Unless the URL is in the exact same place each time, probably best to use a regular expression.

E.g...

"Regular expression to find URLs within a string - Stack Overflow" https://stackoverflow.com/questions/6038061/regular-expression-to-find-urls-within-a-string

tried regex but it doesn't work

Sure it does. Post a sample string containing a URL.

I tried a few examples from your link, they either still allow the whole message through or nothing at all. the URL format is always: https://example.com/page/ it never has www and sometimes has an underscore within /page/.

I copied one of the regex from that page i linked to & it worked...

msg.payload = msg.payload.match(/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/igm);
return msg;
1 Like

Thanks, that works.

I was using the switch node....
Screen Shot 2020-12-28 at 19.45.00

Out of curiosity, how would I do it if it was in the same place every time?

You could use JS string functions like indexOf and/or split to break up the string and find the URL. Just stick with the regex if it works

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.