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
JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("\\?([^#\\s]+)", "g");
const input = "https://example.com/page?utm_source=google&id=42";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
pyPython
import re
pattern = re.compile(r"\?([^#\s]+)")
input_text = "https://example.com/page?utm_source=google&id=42"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\?([^#\s]+)`)
input := `https://example.com/page?utm_source=google&id=42`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
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.