Function: extractRouteParams()
API / @xmachines/play-router / extractRouteParams
function extractRouteParams(pathname, pattern): Record<string, string>;Defined in: router-sync.ts:112
Extract named path parameters from a URL using the URLPattern API.
Takes the pattern string directly — use this when the pattern is already known.
For extraction via a state ID lookup, see RouterBridgeBase.extractParams.
Undefined values (unmatched optional segments, e.g. /settings against
/settings/:section?) are omitted from the returned object.
The pattern is normalized with the same rules RouteMap uses for matching
(hyphenated names like :cat-id become :cat_id for URLPattern), and the
extracted group names are mapped back to the original param names, so
/docs/123 against /docs/:cat-id yields { "cat-id": "123" }.
Values are percent-decoded (john%20doe → john doe), matching framework
router semantics (e.g. vue-router). Malformed sequences are kept raw.
Parameters
| Parameter | Type | Description |
|---|---|---|
pathname | string | The concrete URL pathname (e.g. /profile/alice) |
pattern | string | The URL pattern template (e.g. /profile/:username) |
Returns
Record<string, string>
A record of extracted parameter values, or {} for static patterns.
Throws
When URLPattern is absent and the pattern is parameterized.
Example
import { extractRouteParams } from "@xmachines/play-router";
extractRouteParams("/profile/alice", "/profile/:username");// → { username: "alice" }
extractRouteParams("/settings", "/settings/:section?");// → {} (optional param absent — not included)