Skip to content

GTM Regex Cheatsheet

GTM uses ECMAScript-flavoured regex throughout: in trigger conditions, variable transformations, lookup tables, and regex table variables. This cheatsheet covers the patterns you will reach for constantly. All patterns are tested against the GTM engine.

PatternMatchesNotes
^/blog/Paths starting with /blog/Use ^ to anchor to path start
^/blog$Exactly /blog with no trailing slash$ anchors to end
^/blog/?$/blog and /blog/? makes the trailing slash optional
^/(shop|store)//shop/ and /store/Alternation with | (escaped in some contexts)
/product/[0-9]+/product/123, /product/9876[0-9]+ = one or more digits
/product/[a-z0-9\-]+/product/blue-running-shoesSlug pattern
\?A literal ? in the URLEscape special chars with \
[?&]utm_source=?utm_source= or &utm_source=Matches UTM parameter anywhere in query string
^/thank.you/thank-you, /thank_you, /thankyou. matches any character — may match too broadly
^/thank\-youExactly /thank-youEscape - with \ to be explicit
checkoutAny URL containing “checkout”No anchors = contains match
^https://www\.example\.comFull URL starting with your domainEscape . in domain names
PatternMatchesNotes
^www\.example\.com$Exactly www.example.comUse with {{Page Hostname}} variable
example\.com$www.example.com, shop.example.comMatches any subdomain of example.com
^(www|shop)\.example\.com$Only www and shop subdomainsExplicit subdomain list
staging|localhost|127\.0\.0\.1Development environmentsUse as a blocking trigger
^(?!www\.)Any hostname NOT starting with wwwNegative lookahead — useful for staging blocks
\.co\.uk$UK domain variantsCountry-code TLD matching
PatternMatchesNotes
`.(pdf)(?$)`URLs ending in .pdf
`.(pdf|docx?|xlsx?|pptx?)(?$)`Common office document types
`.(zip|tar.gz|rar)(?$)`Archive files
`.(jpg|jpeg|png|gif|webp|svg)(?$)`Image files
`.(mp4|webm|mov|avi)(?$)`Video files
`.(mp3|wav|ogg|aac)(?$)`Audio files
`.(csv|json|xml)(?$)`Data files
PatternMatchesNotes
[?&]q=([^&]+)The value of ?q= parameterUse capture group in a replacement variable
[?&]utm_source=([^&]+)UTM source valueCombine with regex capture for extraction
[?&]gclid=Any URL with a Google Click IDDetect paid traffic
[?&]fbclid=Any URL with a Facebook Click IDDetect Facebook paid traffic
[?&](utm_source|utm_medium|utm_campaign)=Any UTM parameterCheck if URL has any UTM tags
PatternMatchesNotes
^(Add to Cart|Buy Now)$Exact button text variantsCase-sensitive; add “(ignore case)” operator
(free trial|sign up|get started)CTA phrases anywhere in text
^\s*(.*?)\s*$Text trimmed of leading/trailing whitespaceUse in a Custom JavaScript Variable instead
\d+Any text containing a number
^\d+$Text that is only digits

Use these in Custom HTML tags to scrub event data before it reaches GA4.

PatternDetectsNotes
[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}Email addressesStandard email regex
\b\d{3}[\s.\-]?\d{3}[\s.\-]?\d{4}\bUS phone numbers (various formats)Matches 555-123-4567, 555.123.4567, 5551234567
\+?\d{1,3}[\s\-]?\(?\d{1,4}\)?[\s\-]?\d{1,9}([\s\-]\d{1,9})*International phone numbersMore permissive — adjust for your regions
\b\d{3}\-\d{2}\-\d{4}\bUS Social Security Numbers
\b4[0-9]{12}(?:[0-9]{3})?\bVisa card numbers
\b5[1-5][0-9]{14}\bMastercard numbers
\b3[47][0-9]{13}\bAmerican Express numbers
\b[0-9]{4}[\s\-]?[0-9]{4}[\s\-]?[0-9]{4}[\s\-]?[0-9]{4}\bGeneric 16-digit card pattern
\b[A-Z]{1,2}[0-9R][0-9A-Z]?\s?[0-9][A-Z]{2}\bUK postcodes
\b[0-9]{5}(?:\-[0-9]{4})?\bUS ZIP codes (basic, high false-positive rate)Use sparingly
PatternMatchesNotes
^\d+$Integer: 42, 1000Only digits, entire string
^\d+(\.\d{1,2})?$Decimal with up to 2 places: 49.99
^\d{1,3}(,\d{3})*(\.\d{2})?$Formatted number: 1,234.56European formatting differs
^[+-]?\d+(\.\d+)?$Any positive or negative number
^\$[\d,]+(\.\d{2})?$Dollar amount string: $49.99

GTM variable extraction (use with “Replace” in variables)

Section titled “GTM variable extraction (use with “Replace” in variables)”

These regex patterns are used in the Search/Replace field of GTM variables to extract specific parts of a string.

FindReplaceUse case
.*[?&]q=([^&]*).*$1Extract search query from full URL
^https?://[^/]+(.*)$1Strip protocol and hostname, return path
^([^?#]*).*$1Strip query string and fragment
.*\/([^\/\?]+)(\?.*)?$$1Extract last path segment (filename)
\.([a-zA-Z0-9]+)(\?.*)?$$1Extract file extension
^.*?\/\/([^\/]+)\/.*$$1Extract hostname from full URL

When choosing between GTM’s trigger condition operators:

Use caseOperatorExample
Exact matchequalsPage Path equals /contact
Prefix matchstarts withPage Path starts with /blog
Substring matchcontainsPage Path contains checkout
Pattern matchmatches RegExPage Path matches RegEx ^/(en|fr|de)/
Case-insensitivematches RegEx (ignore case)Click Text matches RegEx (ignore case) add to cart
Exclusiondoes not match RegExPage Hostname does not match RegEx staging|localhost

Use these in Regex Table variables to map URL patterns to readable values:

Pattern → Output
^/blog/ → Blog
^/product/ → Product
^/checkout/ → Checkout
^/account/ → Account
^/$ → Homepage
.* → Other ← Default fallback (put last)

Useful anchors and quantifiers quick reference

Section titled “Useful anchors and quantifiers quick reference”
SymbolMeaningExample
^Start of string^/blog
$End of string\.html$
.Any character except newlinep.ge matches page, p1ge
\dDigit [0-9]order-\d+
\wWord char [a-zA-Z0-9_]\w+
\sWhitespacefirst\sname
\bWord boundary\bcat\b won’t match category
*0 or morecolou*r matches color and colour
+1 or moreid=\d+
?0 or 1 (optional)https?
{n}Exactly n times\d{4}
{n,m}Between n and m times\d{5,10}
[abc]Character class[aeiou]
[^abc]Negated class[^?&]+
(a|b)Alternation(pdf|docx)
(...)Capture group([^&]+)
(?:...)Non-capturing group(?:www\.)?