Advertisement

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.

The pattern
[?&]utm_(source|medium|campaign|term|content)=[^&#]*
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
https://x.co/?utm_source=newsletterMatchfirst paramTry →
/landing?a=1&utm_medium=emailMatchsecond paramTry →
/post?utm_content=hero_btn&fbclid=xyzMatchmixed with other paramsTry →
https://x.co/pricingNo matchuntagged URLTry →
/?UTM_SOURCE=NewsNo matchcase-sensitive by default — see notesTry →
/?source=newsletterNo matchmissing utm_ prefixTry →

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

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

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.

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.