Issue with rewrites regex #70303
Unanswered
lriggle-strib
asked this question in
Help
Replies: 1 comment
-
I tried, directly with const { pathToRegexp } = require("path-to-regexp");
const onlyDigits = "/:slug/:id([0-9]{1,})";
const paths = [
"/some-path",
"/some-path/some-deeper-path/the-deepest-path",
"/some-path/12345",
"/some-path/some-deeper-path",
];
const matcher = pathToRegexp(onlyDigits);
paths.forEach((path) => {
console.log({ path, test: matcher.test(path) });
}); And I got: { path: '/some-path', test: false }
{ path: '/some-path/some-deeper-path/the-deepest-path', test: false }
{ path: '/some-path/12345', test: true }
{ path: '/some-path/some-deeper-path', test: false } Which I think, is what you want? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
I'm trying to build a rewrite that accepts a URL that looks like this:
/some-path/12345
, and route it to a specific folder. I also have a rewrite that picks up things like/some-path/some-deeper-path/the-deepest-path
and/some-path/some-deeper-path
Unfortunately I must be doing the regex wrong in my first rewrite because no matter what I do to try and indicate the
:id
parameter is numeric,/some-path/some-deeper-path
still matches on it.The regex pattern being used matches the example given on this page in the Nextjs documentation. I must be missing something, however, because I cannot get the it to only match on numbers.
I have also tried using other regex patterns to indicate "numbers only, please", like
\d+
,[0-9]+
, etc.I've also looked through the somewhat sparse documentation on the
path-to-regexp
package and not found anything that works.Correct: /some-path
Incorrect: /some-path/some-deeper-path
Correct: /some-path/some-deeper-path/the-deepest-path
Correct: /some-path/12345
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions