Advertisement

Pattern library / Network & URLs

Regex to Match URL Query Strings

Matches ?key=value&key2=value2. Values may contain URL-safe characters. For URL-encoded values, decode first.

The pattern
^\?([A-Za-z0-9_-]+=[^&]*)(&[A-Za-z0-9_-]+=[^&]*)*$
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
?q=hello&page=2Matchtwo paramsTry →
?id=123Matchsingle paramTry →
?a=1&b=2&c=3Matchthree paramsTry →
?q=Matchempty value allowedTry →
q=helloNo matchmissing ?Try →
?=valueNo matchmissing keyTry →

Where to use it

JavaScript

const re = /^\?([A-Za-z0-9_-]+=[^&]*)(&[A-Za-z0-9_-]+=[^&]*)*$/;
re.test('?q=hello&page=2');  // true

Python

import re
re.match(r'^\?([A-Za-z0-9_-]+=[^&]*)(&[A-Za-z0-9_-]+=[^&]*)*$', '?q=hello&page=2')

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

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.