Home > Software design >  Shopify URL UTMs (exclude string past expression end)
Shopify URL UTMs (exclude string past expression end)

Time:06-10

I'm trying to do an expression for Shopify that only matches that of the collection page of a specific collection (named COLLECTION_NAME for simplicity).

I want to account for UTMs but exclude any other part of the URL for example the a specific product.

If you go from a collection to a product page, the collection is still listed in the URL so my condition will trigger basically for all products if the visitor goes from collection to product page.

This is what I currently have:

.*\/collections\/COLLECTION_NAME.*?\/?(?!.*products)

What I want is to exclude anything past the / after the UTM.

I'm testing 3 strings:

  1. https://test.com/collections/COLLECTION_NAME?utm=asdgdsg
  2. https://test.com/collections/COLLECTION_NAME
  3. https://test.com/collections/COLLECTION_NAME/products/PRODUCT_NAME

I want the regex to be matched in the first 2 but NOT the 3rd

CodePudding user response:

You should not use .*? after the COLLECTION_NAME in your pattern.

.*\/collections\/COLLECTION_NAME\/?(?!.*products)

See the preview

CodePudding user response:

You might optionally match the utm part and assert the end of the string:

\S*\/collections\/COLLECTION_NAME(?:\?utm=\w )?$

Regex demo

Or match any character except / or spaces:

\S*\/collections\/COLLECTION_NAME\b[^\/\s]*$

Regex demo

  • Related