Does nextjs middleware matcher uses different regex than usual js regex?
Answered
American black bear posted this in #help-forum
American black bearOP
I'm trying to implement a middleware for my website using nextauth. I have a matcher exported like this:
I have tested this regex in some online regex tester (like regex101). The purpose of the regex is to ignore all paths which start from api,_next/static etc and ignore the ones which are like media files(
I have noticed that in nextjs they used
export const config = { matcher: ["\/((?!api|_next\/static|_next\/image|favicon\.ico|[^\.]+\..+).*)"] }I have tested this regex in some online regex tester (like regex101). The purpose of the regex is to ignore all paths which start from api,_next/static etc and ignore the ones which are like media files(
[^\.]+\..+) . Even though it works in other regex tester it doesn't work in the nextjs.I have noticed that in nextjs they used
. without escaping (in favicon.ico), why?Answered by American black bear
The right way:
I should escape backslashes.
/((?!api|_next/static|_next/image|favicon\\.ico|[^\\.]+\\..+).*)I should escape backslashes.
1 Reply
American black bearOP
The right way:
I should escape backslashes.
/((?!api|_next/static|_next/image|favicon\\.ico|[^\\.]+\\..+).*)I should escape backslashes.
Answer