Extraction
Extract a URL query parameter
Grab a specific query parameter's value from a URL string.
Pattern
Regular expression
[?&]KEY=([^&#]*)How it works
Replace `KEY` with the parameter name you want. The first capture group holds the raw (URL-encoded) value up to the next `&` or `#`.
Matches
- ▸?id=42 → 42
- ▸?a=1&id=42&x=y → 42
- ▸?id=42#frag → 42
Non-matches
- ▸? (no key)
- ▸?id (no =value)
Common gotchas
The captured value is percent-encoded. Decode it with `decodeURIComponent` (JS) / `urllib.parse.unquote` (Python) / `url.QueryUnescape` (Go) before using. In real code, prefer `URLSearchParams` / `urllib.parse.parse_qs` — regex is only a fallback when you cannot parse the URL.
Language snippets
JavaScript
const m = url.match(/[?&]id=([^&#]*)/);
const value = m ? decodeURIComponent(m[1]) : null;Python
from urllib.parse import urlparse, parse_qs
parse_qs(urlparse(url).query).get("id", [None])[0]Go
u, _ := url.Parse(rawURL)
id := u.Query().Get("id")Java
URI u = URI.create(rawURL);
// Parse u.getQuery() manually or use Apache HttpClient's URIBuilderFrequently asked questions
What is the regex for Extract a URL query parameter?
The pattern [?&]KEY=([^&#]*) matches Extract a URL query parameter. Replace `KEY` with the parameter name you want. The first capture group holds the raw (URL-encoded) value up to the next `&` or `#`.
What does this Extract a URL query parameter pattern match?
It matches strings like ?id=42 → 42, ?a=1&id=42&x=y → 42, ?id=42#frag → 42, but rejects ? (no key), ?id (no =value).
How do I use this pattern in JavaScript, Python, Go, Java?
Ready-to-run snippets are provided for JavaScript, Python, Go, Java. Copy the one for your language from the Language snippets section — each wraps the same core pattern in that language's regex API.
What are common pitfalls with a Extract a URL query parameter regex?
The captured value is percent-encoded. Decode it with `decodeURIComponent` (JS) / `urllib.parse.unquote` (Python) / `url.QueryUnescape` (Go) before using. In real code, prefer `URLSearchParams` / `urllib.parse.parse_qs` — regex is only a fallback when you cannot parse the URL.