Matches a pathname or URL against a path pattern. It returns typed, decoded params, or null when it doesn't match.
import { matchPath } from 'mxs';
matchPath('/users/:userId', '/users/42?tab=posts');
// => { params: { userId: '42' } }
matchPath('/files/*', 'https://example.com/files/docs/a%20b.md');
// => { params: { '*': 'docs/a b.md' } }
matchPath('/users/:userId', '/posts/42');
// => nullIn a Node handler, pass req.url directly:
const match = matchPath('/users/:userId', req.url);
if (match) {
match.params.userId; // string
}matchPath(pattern, pathnameOrUrl): { params } | nullParams are inferred from the path pattern: :name is required, :name? is optional, and * is the wildcard.
- Accepts a pathname, a pathname with search and hash, or a full URL. Search and hash are ignored.
- A trailing slash still matches:
/users/42/matches/users/:userId. - Matching is case-sensitive.
- Params are decoded with
decodeURIComponent. - An optional param that is absent is left out of
params. - Search isn't parsed. Use
URLSearchParamsfor that. matchPath(pattern, buildPath(pattern, { params }))returns the sameparams.
Built on the native URLPattern, available in Node 24+ and modern browsers.