0.1.0 • Published 6 months ago
route-path-match v0.1.0
route-path-match - 路由路径匹配
安装
npm i -D route-path-match
# or
pnpm add -D route-path-match
示例
import parsePathTplToMatch from "route-path-match";
const matchParams = parsePathTplToMatch("/:group/:id");
const params = matchParams("/items/aaa");
if (params) {
console.log("path matched", params);
// params -> { group: 'items', id: 'aaa' }
} else {
console.log("path unmatch");
}
路径模板规则
- 其中
(
)
?
等 3 个字符为正则的含义及用法,即分组与可选 - 变量使用形如
:id
或::filePath
即以:
开头的变量名,前者匹配除/
之外的字符,后者匹配任意字符 - 指定变量的正则形如
:id<[0-9]+|all>
其中<
>
包裹的内容会被识别为该变量的正则表达式。其中的反斜杠需要转义,如\\d+
- 变量名只支持
驼峰形式
,比如 :groupId - 路径模板使用变量时,会转换为正则,否则作字符串全等匹配
路径模板示例
0.1.0
6 months ago