Skip to main content

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. Unlike matchRoutes 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 PathPattern object with path, caseSensitive, and end properties
When a string is provided, it’s treated as { 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 parameters
  • pathname - The matched portion of the pathname
  • pathnameBase - The matched pathname before wildcards
  • pattern - 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

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

Notes

  • Pattern matching is case-insensitive by default
  • Parameters can contain any characters except /
  • Wildcards (*) match zero or more segments
  • Use end: false for prefix matching
  • Returns null (not undefined) when there’s no match
  • Trailing slashes are normalized automatically