Home > Enterprise >  Regex for multiple URLs without clear pattern
Regex for multiple URLs without clear pattern

Time:09-09

I'm quite new to using regex so I hope there's someone who can help me out. I want to set up an event on Google Tag Manager through RegEx that fires whenever someone views a page. I'm trying to do this using the Page URL as a parameter so that the event hits, when that URL is visited. Its for around 1400 urls that are in the same sub-folder but have a different page name. For example: https://www.example.com/products/product-name-1, https://www.example.com/products/product-name-2

What would be the best way to group these into one RegEx formula?

I've tried to separate all urls by using the '|' sign without any result. I've also tried this format, without any luck: (^/page-url-1/$|^/page-url-1/$|^/page-url-1/$|^/page-url-1/$)

CodePudding user response:

A couple things are happening with your attempt. First, you aren't escaping the '/'. This is a reserved or special character and you will need to precede it with a \ to tell the engine that you want that specific character. It would look like this:

\/products\/page-url-1

I am assuming you are using a {{Page Path}} so the above would match for any paths that contain /products/page-url-1.

If you want the event to fire on all pages within the /products directory, there is an easier way of doing this.

\/products\/.*

what this will do is match any pages within your /products directory. If you have a landing page on /products, this will be omitted from the firing. The '.' means it will then match any character after the / and '*' means it can do this unlimited times.

EDIT:

Since you aren't looking for all the products pages, you can you a matching group and list them all. I suspect that all the product names will be different enough and not share any common path elements so you will have to list out the ones want.

\/products\/(product-url-1|product-url-2|product-url-3).*
  • Related