The regexparam library allows for passing RegExp through parse. I think it would be a useful feature to be able to use custom regular expressions for routes.
If it doesn't match, it will continue on to the 404 page.
This uses a named capture group so it could be a direct drop in for "/:id".
Alternatively, a normal regex could be used:
/^[/](\d+)$/
// Then consumed like:
const params = useParams();
const id = params[1];
This allows for lifting validation out of routes.
Patterns like route prefixes (/@:username) and multiple params in a single segment (/:id-:opt) becomes possible.
And since it builds upon functionality that is already here, it won't cost much to implement.
The
regexparam
library allows for passingRegExp
throughparse
. I think it would be a useful feature to be able to use custom regular expressions for routes.For example:
id
will match anything. But if we know that an ID will only be digits, it would be nice to use a regex to validate it:If it doesn't match, it will continue on to the 404 page.
This uses a named capture group so it could be a direct drop in for
"/:id"
.Alternatively, a normal regex could be used:
This allows for lifting validation out of routes. Patterns like route prefixes (
/@:username
) and multiple params in a single segment (/:id-:opt
) becomes possible. And since it builds upon functionality that is already here, it won't cost much to implement.