Use existing RegExps with parse()
mainIf you pass a RegExp directly to parse(), the module does not parse or manipulate it. It treats the input as an authoritative pattern.
Important implications:
regexparamhas no insight into the route structure.- The returned
keysproperty will always befalse. - The returned
patternwill be identical to your input. - You are responsible for managing and parsing your own keys (e.g., using named capture groups or manual array destructuring).
import { parse } from 'regexparam';
// Using Named Capture Groups (requires environment support for ES2018+)
const named = parse(/^\/posts[/](?<year>[0-9]{4})[/](?<month>[0-9]{2})[/](?<title>[^\/]+)/i);
const { groups } = named.pattern.exec('/posts/2019/05/hello-world');
//=> { year: '2019', month: '05', title: 'hello-world' }
// Using standard capture groups
const manual = parse(/^\/posts[/]([0-9]{4})[/]([0-9]{2})[/]([^\/]+)/i);
const [url, year, month, title] = manual.pattern.exec('/posts/2019/05/hello-world');
// year: 2019, month: 05, title: hello-world