# 错误：无效的中间件匹配器。模式 'X' 不是有效的正则表达式或路径模式。

- **ID:** `nextjs/invalid-middleware-matcher-regex`
- **领域:** nextjs
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

middleware.config 中的匹配器数组包含一个格式错误的正则表达式或路径模式，Next.js 无法解析。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| next@13.4.0 | active | — | — |
| next@14.0.0 | active | — | — |
| next@14.2.0 | active | — | — |

## 解决方案

1. ```
   使用正确的 glob 语法进行路径匹配。例如，匹配所有 API 路由：

export const config = {
  matcher: '/api/:path*',
};

对于多个模式，使用数组：
export const config = {
  matcher: ['/api/:path*', '/admin/:path*'],
};
   ```
2. ```
   如果需要复杂匹配，使用自定义中间件函数手动检查请求 URL，而不是依赖匹配器。
   ```
3. ```
   使用 Next.js 文档验证你的模式：模式使用简化的 glob，其中 '*' 匹配任何路径段，':param' 捕获命名参数。
   ```

## 无效尝试

- **** — Next.js matcher patterns use a simplified glob syntax, not full regex. Forward slashes are reserved for path segments. (80% 失败率)
- **** — Characters like '.' or '*' need escaping in regex but Next.js matcher uses glob where '*' is a wildcard, causing ambiguity. (70% 失败率)
- **** — An empty array matches no routes, effectively disabling the middleware, which may not be the intended behavior. (50% 失败率)
