Skip to content

matchPath() function

Home > @rimitive/router > matchPath

Matches a URL path against a route pattern (exact match)

Supports exact string matching, path parameters using :paramName syntax, and wildcard ’*’ for catch-all

Signature:

matchPath: (pattern: string, path: string) => RouteMatch | null

Parameter

Type

Description

pattern

string

path

string

Returns:

RouteMatch | null

import { matchPath } from '@rimitive/router';
// Exact match
matchPath('/about', '/about');
// { path: '/about', params: {} }
// Path parameters
matchPath('/products/:id', '/products/123');
// { path: '/products/123', params: { id: '123' } }
// Multiple parameters
matchPath('/blog/:year/:slug', '/blog/2024/hello-world');
// { path: '/blog/2024/hello-world', params: { year: '2024', slug: 'hello-world' } }
// Wildcard
matchPath('*', '/any/path');
// { path: '/any/path', params: {} }
// No match
matchPath('/about', '/contact');
// null