Pattern library / URLs & Marketing
Regex to Remove Query Parameters from a URL
Tracking parameters turn /pricing into /pricing?utm_source=nl&session=abc — and every variant becomes a separate row in your analytics. Regex replace (not match) is the cleanup tool: drop the whole query string, or remove only the noisy parameters and keep the rest.
How the pattern works
\?.*$with replacement empty — everything from the first question mark to the end disappears.- Keep the fragment but kill the query: replace
\?[^#]*instead —/a?b=1#sbecomes/a#s. - Remove only utm parameters: replace
\?(utm_[^=&]+=[^&]*&?)*, then clean a leftover\?$and dangling\?&— two passes keep it honest (test both in the tester). - In GA4 itself you cannot rewrite data with regex — use this pattern in your ETL (BigQuery REGEXP_REPLACE) or tag manager preprocessing instead.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
/pricing?utm_source=nl | Match | replace → /pricing | Try → |
https://x.co/a?b=1&c=2#frag | Match | query dropped, fragment too (see notes) | Try → |
/clean/path | No match | no query — nothing to replace | Try → |
/search?q=regex&t=10 | Match | whole query removed | Try → |
/blog/post-2 | No match | unchanged by design | Try → |
/?a=1 | Match | root with query → / | Try → |
Where to use it
JavaScript
const clean = url.replace(/\?.*$/, ''); // drop query + fragment const keepFrag = url.replace(/\?[^#]*/, ''); // keep #fragment
Python (pandas URL cleanup)
import re df['page'] = df['page'].str.replace(r'\?.*$', '', regex=True)
BigQuery (group clean pages)
SELECT REGEXP_REPLACE(page_location, r'\?.*$', '') AS clean_url,
COUNT(*) AS views
FROM events GROUP BY 1 ORDER BY 2 DESC
Common mistakes
- Anchors:
.*$needs thesflag in JS if a newline can appear — URLs never should, but logs lie. - Order matters when removing selected params: removing
&x=1first can leave a danglingx=1&— handle both boundary shapes. - Do this at reporting time (BigQuery/views), not on live tags — retroactive cleanup is exactly why you keep raw data.
FAQ
How do I remove only utm parameters and keep others?
Two replaces: first drop utm pairs (\?(utm_[^=&]*=[^&]*&?)+ → reduce to ? or empty), then fix dangling separators (?& and trailing &). Test each pass in the tester before scripting it.
Why does my replace remove the fragment too?
Because \?.* runs to the end of string. Use \?[^#]* to stop at the # and keep the fragment.
Can I use this inside Google Analytics 4?
GA4 filters only match, they do not rewrite. Run REGEXP_REPLACE in BigQuery exports or normalize in your tag manager before data lands.
Related patterns
UTM Parameter Regex — Match & Extract utm_* Tags · 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.