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/.+
| URL | Matches? |
|---|---|
/products/blue-shoes | Yes |
/products/red-boots-size-10 | Yes |
/products/ | No (nothing after the slash) |
/products | No |
/collections/products/summer | No (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)
| URL | Matches? |
|---|---|
/thank-you | Yes |
/confirmation | Yes |
/order-complete | Yes |
/thank-you-page | Yes (contains “thank-you”) |
/about | No |
Explanation: The pipe | means “or.” Parentheses group the alternatives.
3. Match URL with Optional Query Parameters
Pattern: /checkout/success(\?.*)?$
| URL | Matches? |
|---|---|
/checkout/success | Yes |
/checkout/success?order=123 | Yes |
/checkout/success?order=123&ref=email | Yes |
/checkout/success/next-step | No |
Explanation: (\?.*)? means “optionally, a question mark followed by anything.” The $ anchors to the end.
4. Match Pages Under a Section (Any Depth)
Pattern: ^/blog(/.*)?$
| URL | Matches? |
|---|---|
/blog | Yes |
/blog/ | Yes |
/blog/my-post | Yes |
/blog/category/seo/my-post | Yes |
/about/blog | No (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)(\?.*)?$
| URL | Matches? |
|---|---|
/files/report.pdf | Yes |
/downloads/data.csv | Yes |
/assets/guide.pdf?v=2 | Yes |
/page.html | No |
/images/photo.jpg | No |
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
| URL | Matches? |
|---|---|
/page?utm_source=facebook | Yes |
/page?ref=abc&utm_source=facebook | Yes |
/page?utm_source=facebook&utm_medium=cpc | Yes |
/page?utm_source=google | No |
Explanation: [?&] matches either ? (first parameter) or & (subsequent parameter).
7. Match Numeric IDs in URLs
Pattern: /order/[0-9]+
| URL | Matches? |
|---|---|
/order/12345 | Yes |
/order/1 | Yes |
/order/abc | No |
/order/ | No |
Explanation: [0-9]+ matches one or more digits.
8. Exclude Specific Pages
Pattern (negative): ^/blog/(?!draft-).*
| URL | Matches? |
|---|---|
/blog/my-published-post | Yes |
/blog/seo-guide | Yes |
/blog/draft-new-post | No |
/blog/draft-test | No |
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
| URL | Matches? |
|---|---|
https://yoursite.com/pricing | Yes |
https://www.yoursite.com/pricing | Yes |
http://yoursite.com/pricing | Yes |
https://other.com/pricing | No |
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 Class | Matches? |
|---|---|
btn-primary | Yes |
nav-link btn-primary large | Yes (contains the match) |
cta-button-v2 | Yes (contains “cta-button”) |
regular-link | No |
For more click tracking patterns, see our GTM click tracking guide.
Regex Quick Reference
Characters
| Pattern | Meaning | Example |
|---|---|---|
. | Any single character | a.c matches “abc”, “a1c” |
\d | Any digit (0-9) | \d{3} matches “123” |
\w | Any word character (letters, digits, underscore) | \w+ matches “hello_123” |
\s | Any whitespace | hello\sworld matches “hello world” |
\. | Literal dot | file\.pdf matches “file.pdf” |
Quantifiers
| Pattern | Meaning | Example |
|---|---|---|
* | Zero or more | ab*c matches “ac”, “abc”, “abbc” |
+ | One or more | ab+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
| Pattern | Meaning | Example |
|---|---|---|
^ | 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
| Pattern | Meaning | Example |
|---|---|---|
| `(a | b)` | 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)
Fire on Outbound Link Clicks
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:
- GTM → Preview
- Visit matching and non-matching URLs
- Check the trigger conditions in the Preview panel
- If the regex doesn’t match, adjust and retest
Online Regex Testers
Use regex101.com to test patterns before putting them in GTM:
- Select “JavaScript” as the flavor (GTM uses JavaScript regex)
- Enter your pattern
- Enter test strings
- Check matches and non-matches
Common Mistakes
| Mistake | Problem | Fix |
|---|---|---|
| Not escaping dots | /products/shoes.html matches /products/shoesXhtml | Use \. for literal dots |
| Forgetting anchors | /blog matches /about/blog | Use ^/blog to anchor to start |
| Case sensitivity | /Thank-You doesn’t match /thank-you | Use “matches RegEx (ignore case)“ |
| Greedy matching | .* captures too much | Use .*? for lazy matching |
| Unescaped special chars | product? makes “t” optional | Use \? for literal question mark |
Debugging Regex in GTM Variables
If your RegEx Table variable isn’t returning the expected output:
- Create a Custom JavaScript variable that logs the input:
function() {
console.log('RegEx input:', {{Page Path}});
return {{Page Path}};
}
-
Check the GTM Preview panel’s Variables tab to see the exact value being matched
-
Test the exact value (copy-paste from Preview) in regex101.com
-
Remember: GTM regex matches against the FULL value unless you use anchors.
blogmatches any URL containing “blog” — use^/blogto 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.