Next.js Discord

Discord Forum

Does nextjs middleware matcher uses different regex than usual js regex?

Answered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
I'm trying to implement a middleware for my website using nextauth. I have a matcher exported like this:
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: /((?!api|_next/static|_next/image|favicon\\.ico|[^\\.]+\\..+).*)
I should escape backslashes.
View full answer

1 Reply

American black bearOP
The right way: /((?!api|_next/static|_next/image|favicon\\.ico|[^\\.]+\\..+).*)
I should escape backslashes.
Answer