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
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "number": [],
          "string": [
            {
              "name": "header.location",
              "value": "https://www.engadget.com/arcane-netflix-league-legends-review-020059350.html?src=rss?utm_source=Telegram&utm_medium=IXNews&utm_campaign=PerspectiveIX.com"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        410,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "// This function creates a new variable that has the extra fluff removed from the URL\nitem.header.cleaned = item.header.location.match(/^[^?]+/);\nreturn item;"
      },
      "name": "FunctionItem",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        560,
        120
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "cleaned",
              "value": "={{$json[\"header\"][\"location\"].match(/^[^?]+/)}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        560,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{$json[\"header\"][\"location\"].match(/^[^?]+/)[0]}}",
        "responseFormat": "string",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        560,
        450
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "FunctionItem",
            "type": "main",
            "index": 0
          },
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          },
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

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