Can't use Regex in IF to remove tracking part of an URL

Hi, please help me out with this:

  • Background: I’m trying to remove the tracking part of a shortened URL by using n8n. For example: A shortened link like abc.dx/aaaa will be revealed through HTTP Request, and will be come abcde.com/aaaaa?utm=trackinglink&ad-goes-here&other-tracking. I want to remove the tracking part using Regex in IF function.

  • Here’s the result when I reveal the true URL:

  • Here’s the result when I use Regex in IF function. And I’m sure the Regex is working (tested on Regex101)

I expect it would reveal only the fresh link without tracking URL. Please help me with this. Thank you so much!

Hey there,

Using an If Node isn’t the way to do what you want but it would let you know if the link contains the tracking code.

If you use the expression editor on any node that you want to use the link in you can remove it there at the time you use it or you could use a set node and set a variable that has the code removed.

Thanks a lot @Jon ! You mentioned “remove the code”, what do you mean by that? Would you mind explaining more about that?
Thank you.

Hey @miniduke,

By remove the code I mean pop in your regex to remove the tracking code. I can get a quick example together a bit later today if needed.

1 Like

Hi @Jon ,

Thanks for your reply! I use this to remove the tracking code. Tested in regex101 and it worked. Just don’t know why it doesn’t work in this IF node.

It would be great if you can make a example of how to handle this with n8n. Thank you so much!

Hey @miniduke,

The example below shows 3 ways you can extract the URL, The first Set node sets a variable with your Engadget URL then we call 3 other nodes…

The Function Item node adds a new variable called cleaned which contains just the URL

The Set Node sets a variable called Cleaned using the same regex option.

and the HTTP request is there to get the page using the same regex option so you can see how you could use it in a normal node.

The actual magic is performed by adding .match(/^[^?]+/) to the end of the input variable, I think where you were going wrong was you thought the If Node sets a variable which is not what an If node or an If statement in programming is for. You would use the If node to see if something matches a certain pattern it doesn’t do any clever extraction you would normally need to handle that after.

Workflow Example

Hopefully this helps and gets you on your way to automating your task :+1:

2 Likes

Hi @Jon,

You’re my savior. It works perfectly!

Thank you so much for your time and effort! Send lots of hugs…

2 Likes

No problem at all, I have added that workflow to my notes as well in case someone else asks in the future.

2 Likes

Hi @Jon,

Can you please help in extracting the real URL from this using SET node? All our URLs are coming with Outlook safelinks initial. How can we remove the highlighted part of the URL?

Hey @saikatdas,

Looking at that if you want the rest of the URL as well you could try using a split on the url= but you have a few URLs there so it might be worth adding an Items List node first and split out URL then add your set node or you could use a code node and do it in a loop.

Hi @Jon,

Can you please share the Javascript to remove the safelinks part of the URL? I tried using ITEM List node but then what should be SET node value?
ITEM List node result

Workflow