Summary
Performs pattern matching on a URL pathname and returns information about the match. Useful for checking if a path matches a pattern and extracting URL parameters. UnlikematchRoutes which works with route trees, matchPath works with a single path pattern.
Signature
Parameters
PathPattern<Path> | string
required
The pattern to match against. Can be:
- A string path pattern (e.g.,
/users/:id) - A
PathPatternobject withpath,caseSensitive, andendproperties
{ path, caseSensitive: false, end: true }.string
required
The URL pathname to match against the pattern (e.g.,
/users/123).PathPattern Object
Returns
PathMatch<ParamKey> | null
A match object if the pattern matches, or
null if it doesn’t match.The match object contains:params- Extracted URL parameterspathname- The matched portion of the pathnamepathnameBase- The matched pathname before wildcardspattern- The pattern that was matched
Examples
Basic string pattern
Multiple parameters
Wildcard pattern
Case-sensitive matching
Partial matching (prefix)
Full vs. partial matching
Common Use Cases
Active link styling
Conditional rendering based on route
Extracting params from pathname
Route-based permissions
Pattern validation
Building breadcrumbs
Pattern Syntax
Dynamic segments
Optional segments
Wildcards
Static segments
Type Safety
TypeScript automatically infers parameter types:Performance
matchPath compiles patterns into regular expressions and caches them internally for performance. Repeated matches against the same pattern are fast.
Comparison with matchRoutes
Related Functions
matchRoutes- Match against a route treegeneratePath- Generate paths from patternsresolvePath- Resolve relative paths
Notes
- Pattern matching is case-insensitive by default
- Parameters can contain any characters except
/ - Wildcards (
*) match zero or more segments - Use
end: falsefor prefix matching - Returns
null(notundefined) when there’s no match - Trailing slashes are normalized automatically