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.
How the pattern works
\?— literal question mark.[A-Za-z0-9_-]+— key (alphanumeric + _ -).=[^&]*— equals + value (anything but &).(&...)*— additional params prefixed with &.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
?q=hello&page=2 | Match | two params | Try → |
?id=123 | Match | single param | Try → |
?a=1&b=2&c=3 | Match | three params | Try → |
?q= | Match | empty value allowed | Try → |
q=hello | No match | missing ? | Try → |
?=value | No match | missing key | Try → |
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.
FAQ
Related patterns
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.