GTM Regex Guide: Pattern Matching for Triggers

Regex in GTM lets you match URLs, clicks, and data layer values with flexible patterns instead of exact strings. Here are the patterns you'll actually use.

GTMGoogle Tag ManagerregextriggersURL matchingpattern matching

You need a GTM trigger that fires on /products/shoes-123, /products/boots-456, and every other product page — but not /products (the category page). Or you need to match URLs with UTM parameters regardless of their position in the query string. Or you need to fire a tag on pages that contain “thank-you” or “confirmation” or “order-complete” in the URL.

All of these require regex (regular expressions). GTM supports regex in trigger conditions, and once you learn the 10 patterns that cover 95% of use cases, you won’t need to create separate triggers for every URL variation.

Where Regex Works in GTM

Triggers

When creating a trigger condition, the operator dropdown includes “matches RegEx” and “matches RegEx (ignore case)“:

Page URL → matches RegEx → your-pattern-here

Variables

The RegEx Table variable type lets you map regex patterns to values:

Input: {{Page Path}}
Pattern: /blog/.*     → Output: "Blog"
Pattern: /products/.* → Output: "Products"
Pattern: /cart.*      → Output: "Cart"
Default:              → Output: "Other"

Filters

In tag configuration (especially for GA4), you can use regex to filter which events fire.

The 10 Patterns You’ll Actually Use

1. Match Any Product Page

Pattern: /products/.+

URLMatches?
/products/blue-shoesYes
/products/red-boots-size-10Yes
/products/No (nothing after the slash)
/productsNo
/collections/products/summerNo (doesn’t start with /products/)

Explanation: .+ means “one or more of any character.” So /products/ followed by at least one character.

2. Match Multiple URL Patterns (OR)

Pattern: /(thank-you|confirmation|order-complete)

URLMatches?
/thank-youYes
/confirmationYes
/order-completeYes
/thank-you-pageYes (contains “thank-you”)
/aboutNo

Explanation: The pipe | means “or.” Parentheses group the alternatives.

3. Match URL with Optional Query Parameters

Pattern: /checkout/success(\?.*)?$

URLMatches?
/checkout/successYes
/checkout/success?order=123Yes
/checkout/success?order=123&ref=emailYes
/checkout/success/next-stepNo

Explanation: (\?.*)? means “optionally, a question mark followed by anything.” The $ anchors to the end.

4. Match Pages Under a Section (Any Depth)

Pattern: ^/blog(/.*)?$

URLMatches?
/blogYes
/blog/Yes
/blog/my-postYes
/blog/category/seo/my-postYes
/about/blogNo (doesn’t start with /blog)

Explanation: ^ anchors to the start. (/.*)? optionally matches a slash followed by anything.

5. Match Specific File Extensions (Downloads)

Pattern: \.(pdf|csv|xlsx|docx|zip)(\?.*)?$

URLMatches?
/files/report.pdfYes
/downloads/data.csvYes
/assets/guide.pdf?v=2Yes
/page.htmlNo
/images/photo.jpgNo

Explanation: \. matches a literal dot. The group matches specific extensions. The optional query string handles cache-busted URLs.

This pattern is essential for tracking file downloads in GTM — combine it with a Click URL trigger to capture all download clicks.

6. Match UTM Parameters (Regardless of Position)

Pattern: [?&]utm_source=facebook

URLMatches?
/page?utm_source=facebookYes
/page?ref=abc&utm_source=facebookYes
/page?utm_source=facebook&utm_medium=cpcYes
/page?utm_source=googleNo

Explanation: [?&] matches either ? (first parameter) or & (subsequent parameter).

7. Match Numeric IDs in URLs

Pattern: /order/[0-9]+

URLMatches?
/order/12345Yes
/order/1Yes
/order/abcNo
/order/No

Explanation: [0-9]+ matches one or more digits.

8. Exclude Specific Pages

Pattern (negative): ^/blog/(?!draft-).*

URLMatches?
/blog/my-published-postYes
/blog/seo-guideYes
/blog/draft-new-postNo
/blog/draft-testNo

Explanation: (?!draft-) is a negative lookahead — it matches only if “draft-” does NOT follow.

Note: GTM’s regex engine supports lookaheads, but test carefully in Preview Mode. Not all regex features work identically across browsers.

9. Match Domain + Path (Full URL)

Pattern: https?://(www\.)?yoursite\.com/pricing

URLMatches?
https://yoursite.com/pricingYes
https://www.yoursite.com/pricingYes
http://yoursite.com/pricingYes
https://other.com/pricingNo

Explanation: https? matches http or https. (www\.)? optionally matches www. \. matches literal dots.

10. Match Click Classes or IDs

When using Click triggers, match against {{Click Classes}} or {{Click ID}}:

Pattern: (btn-primary|cta-button|add-to-cart)

Click ClassMatches?
btn-primaryYes
nav-link btn-primary largeYes (contains the match)
cta-button-v2Yes (contains “cta-button”)
regular-linkNo

For more click tracking patterns, see our GTM click tracking guide.

Regex Quick Reference

Characters

PatternMeaningExample
.Any single charactera.c matches “abc”, “a1c”
\dAny digit (0-9)\d{3} matches “123”
\wAny word character (letters, digits, underscore)\w+ matches “hello_123”
\sAny whitespacehello\sworld matches “hello world”
\.Literal dotfile\.pdf matches “file.pdf”

Quantifiers

PatternMeaningExample
*Zero or moreab*c matches “ac”, “abc”, “abbc”
+One or moreab+c matches “abc”, “abbc” (not “ac”)
?Zero or one (optional)colou?r matches “color” and “colour”
{3}Exactly 3\d{3} matches “123”
{2,5}Between 2 and 5\d{2,5} matches “12” through “12345”

Anchors

PatternMeaningExample
^Start of string^/blog matches “/blog/post” but not “/about/blog”
$End of string\.pdf$ matches “file.pdf” but not “file.pdf?v=1”

Groups and Alternation

PatternMeaningExample
`(ab)`a or b
[abc]Any one of a, b, c[aeiou] matches any vowel
[^abc]NOT a, b, or c[^0-9] matches non-digits
[a-z]Range a through z[a-zA-Z] matches any letter

Real-World GTM Trigger Recipes

Fire on All Blog Posts But Not the Blog Index

Trigger: Page View
Condition: Page Path → matches RegEx → ^/blog/.+

Fire on Product Pages with Specific Categories

Trigger: Page View
Condition: Page URL → matches RegEx → /products/(shoes|boots|sandals)/

Fire on Thank-You Pages Across Multiple Formats

Trigger: Page View
Condition: Page Path → matches RegEx (ignore case) → (thank[-_]?you|confirmation|order[-_]?complete|success)
Trigger: Click - Just Links
Condition: Click URL → does NOT match RegEx → ^https?://(www\.)?yoursite\.com

Fire on PDF/CSV Download Clicks

Trigger: Click - Just Links
Condition: Click URL → matches RegEx → \.(pdf|csv|xlsx|zip)(\?.*)?$

Fire on Pages with Specific UTM Campaign

Trigger: Page View
Condition: Page URL → matches RegEx → [?&]utm_campaign=spring_sale_2026

Testing Your Regex

GTM Preview Mode

The most reliable test. Enable Preview Mode, visit the pages your regex should match, and check whether the trigger fires:

  1. GTM → Preview
  2. Visit matching and non-matching URLs
  3. Check the trigger conditions in the Preview panel
  4. If the regex doesn’t match, adjust and retest

Online Regex Testers

Use regex101.com to test patterns before putting them in GTM:

  1. Select “JavaScript” as the flavor (GTM uses JavaScript regex)
  2. Enter your pattern
  3. Enter test strings
  4. Check matches and non-matches

Common Mistakes

MistakeProblemFix
Not escaping dots/products/shoes.html matches /products/shoesXhtmlUse \. for literal dots
Forgetting anchors/blog matches /about/blogUse ^/blog to anchor to start
Case sensitivity/Thank-You doesn’t match /thank-youUse “matches RegEx (ignore case)“
Greedy matching.* captures too muchUse .*? for lazy matching
Unescaped special charsproduct? makes “t” optionalUse \? for literal question mark

Debugging Regex in GTM Variables

If your RegEx Table variable isn’t returning the expected output:

  1. Create a Custom JavaScript variable that logs the input:
function() {
  console.log('RegEx input:', {{Page Path}});
  return {{Page Path}};
}
  1. Check the GTM Preview panel’s Variables tab to see the exact value being matched

  2. Test the exact value (copy-paste from Preview) in regex101.com

  3. Remember: GTM regex matches against the FULL value unless you use anchors. blog matches any URL containing “blog” — use ^/blog to match only URLs starting with “/blog.”

Checklist

  • Regex tested in regex101.com (JavaScript flavor)
  • Anchors used where needed (^ for start, $ for end)
  • Dots escaped when matching literal dots
  • Case sensitivity setting correct (ignore case for URLs)
  • Tested in GTM Preview Mode on matching AND non-matching pages
  • No overly broad patterns that match unintended pages

Regex turns one trigger into a flexible pattern that handles dozens of URL variations. Master these 10 patterns and you’ll rarely need to create single-page triggers again.

Not sure if your GTM triggers are matching correctly? Run a free scan — we check your tag configuration, trigger setup, and firing conditions.