Webflags: g
URL Query String
Extract the query string portion of a URL (everything between `?` and `#` or end-of-string).
Try it in RegexPro →Available in
Pattern
regexengine-agnostic
\?([^#\s]+) (flags: g)Raw source: \?([^#\s]+)
How it works
\? matches the literal question mark. ([^#\s]+) captures everything up to (but not including) a `#` (fragment) or whitespace (URL boundary). Use URL parsing libraries for production, but this regex is handy for quick log scraping.
Examples
Input
https://example.com/page?utm_source=google&id=42Matches
?utm_source=google&id=42
Input
/api?token=abc#sectionMatches
?token=abc
Input
no query string hereNo match
—Common use cases
- •Log analysis — extracting tracking params
- •URL rewriting / canonicalisation tooling
- •Detecting PII leaked in query strings
- •Marketing attribution parsing
Related patterns
URL Path Segment
WebMatch individual `/segment` parts of a URL path, capturing each one.
URL Slug
WebValidate URL slugs: lowercase letters and digits separated by single hyphens, no leading/trailing hyphens.
URL Validation
WebMatch http and https URLs with optional www prefix, paths, query strings, and fragments.
GitHub Repository URL
WebMatch GitHub repository URLs and capture the owner and repo segments.