DevKits

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 URIBuilder

Related patterns

Try any pattern live in the Regex Tester