Pattern library / URLs & Marketing
UTM Parameter Regex (match & extract utm_*)
Campaign tags are five predictable keys — utm_source, utm_medium, utm_campaign, utm_term, utm_content — but they arrive at any position, in any order, mixed with other parameters. These patterns handle the three jobs marketers actually need: detect a tagged URL, extract a specific tag, and validate tag hygiene.
[?&]utm_(source|medium|campaign|term|content)=[^&#]*Load this pattern into the tester →
How the pattern works
[?&]— the tag must start right after?or an&, so it matches at any position in the query string.utm_(source|medium|campaign|term|content)— the five standard keys; add your own via the alternation.[^&#]*— the value runs until the next parameter or the fragment.- Case: UTM parameters are case-sensitive in most tools — the default case-sensitive match mirrors reality. (GA4 auto-parses utm_* natively; these patterns shine in BigQuery, Sheets and link QA scripts.)
- Extract one tag: wrap it all and capture the value —
[?&]utm_source=([^&#]*)→ group 1.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
https://x.co/?utm_source=newsletter | Match | first param | Try → |
/landing?a=1&utm_medium=email | Match | second param | Try → |
/post?utm_content=hero_btn&fbclid=xyz | Match | mixed with other params | Try → |
https://x.co/pricing | No match | untagged URL | Try → |
/?UTM_SOURCE=News | No match | case-sensitive by default — see notes | Try → |
/?source=newsletter | No match | missing utm_ prefix | Try → |
Where to use it
JavaScript (tag audit)
const hasUtm = /[?&]utm_(source|medium|campaign|term|content)=[^&#]*/.test(url); const src = (url.match(/[?&]utm_source=([^&#]*)/) || [])[1];
Python (Sheets-style cleanup)
import re
UTM = re.compile(r'[?&]utm_(source|medium|campaign|term|content)=[^&#]*')
vals = UTM.findall('https://x.co/?utm_source=nl&utm_medium=email')
BigQuery (campaign grouping)
SELECT REGEXP_EXTRACT(page_location, r'[?&]utm_campaign=([^&#]*)') AS campaign,
COUNT(*) AS sessions
FROM events GROUP BY 1
Common mistakes
- Lowercase the values, not the keys: utm_medium=Email vs email splits reports — normalize in SQL, do not case-fold the pattern blindly.
- Double-tagging (
?utm_source=a?utm_source=b) is malformed, not matched twice — QA links before sending. - fbclid / gclid / msclkid are not utm_* — extend the alternation:
[?&](utm_[a-z]+|fbclid|gclid)=.
FAQ
How do I extract just the utm_source value?
[?&]utm_source=([^]*) — group 1 is the value. Test it live, then use it in JS match(), Python re.search() or REGEXP_EXTRACT in BigQuery.
Why does my uppercase UTM_SOURCE not match?
The pattern is case-sensitive, matching how most platforms treat parameter names. Add the i flag only if your source system genuinely emits mixed-case keys.
Can I use utm regex in GA4 itself?
GA4 parses utm_* automatically into session campaign dimensions — you rarely filter on the raw URL. Use these patterns in BigQuery, Sheets, link-builders and QA scripts.
Related patterns
Remove Query Parameters with Regex (Replace Example) · URL Regex — Match http/https Links (Practical) · Extract Domain from URL — Regex Capture Group
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.