Advertisement

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.

The pattern
\?.*$
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
/pricing?utm_source=nlMatchreplace → /pricingTry →
https://x.co/a?b=1&c=2#fragMatchquery dropped, fragment too (see notes)Try →
/clean/pathNo matchno query — nothing to replaceTry →
/search?q=regex&t=10Matchwhole query removedTry →
/blog/post-2No matchunchanged by designTry →
/?a=1Matchroot 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

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 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.

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.