简体   繁体   English

使用 path-to-regexp 匹配尽可能多的 URL 路由参数?

[英]Match as many URL route parameters as possible with path-to-regexp?

I would like a means to match as many URL parameters as possible example我想要一种方法来匹配尽可能多的 URL 参数示例

/teams/:teamID/players/:playerID/seasons/:seasonID/detail

would match anything starting with /teams and match as many parameters present, up to /detail (and only up to)将匹配以 /teams 开头的任何内容并匹配存在的尽可能多的参数,直到 /detail(并且仅达到)

/Giants => { teamID: null, playerID: null, seasonID: null }
/Giants/123 => { teamID: 123, playerID: null, seasonID: null }
/Giants/123/players/ => same as above
/Giants/123/players/456/seasons/2020/detail => { teamId: 123, playerID: 456, seasonID: 2020 }
/Giants/123/players/446/seasons/2020 => same as above

and not match并且不匹配

/Giants/123/players/456/seasons/2020/detail/12345

I'm using path-to-regexp .我正在使用path-to-regexp

If there is a possibility of not using regex, you can probably split the URL into an array using / as a separator.如果有可能不使用正则表达式,您可以使用/作为分隔符将 URL 拆分为一个数组。 Get every element of the array that's after team all the way until the details .获取数组中的每一个元素,它一直在team之后,直到details

I found a nice library that does exactly what I need, is extremely lite and uses well-tested pieces of Backbone.js to do the pattern matching:我找到了一个很好的库,它完全符合我的需要,非常精简,并使用经过良好测试的 Backbone.js 片段来进行模式匹配:

https://github.com/HenrikJoreteg/feather-route-matcher https://github.com/HenrikJoreteg/feather-route-matcher

Examples:例子:

pattern: '/users/:id' 
url: '/something-else' 
extracted params: nothing, because it won't match

pattern: '/users/:id' 
url: '/users/scrooge-mc-duck' 
extracted params: {id: 'scrooge-mc-duck'}

pattern: '/users/:id' 
url: '/users/47' 
extracted params: {id: '47'}

pattern: '/schools/:schoolId/teachers/:teacherId' 
url: '/schools/richland/teachers/47' 
extracted params: {schoolId: 'richland', teacherId: '47'}

This provided me exactly what I wanted and needed这为我提供了我想要和需要的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将路径与正则表达式与路径到正则表达式匹配? - How to match path with regexp with path-to-regexp? vue-router path-to-regexp与可选的路由路径参数组不匹配 - vue-router path-to-regexp does not match optional route path parameter group 如何将vue-router中的可选段与路径到正则表达式匹配 - How to match an optional segment in vue-router with path-to-regexp 寻找与正则表达式路径完全相反的方法 - Looking for the exact opposite of path-to-regexp 如何使用路径到正则表达式来匹配所有不以 /api/ 开头的路径? - how to use path-to-regexp to match all paths that's not starting with /api/? 无法找到模块“路径到正则表达式” - Unable to find module 'path-to-regexp' 路径到正则表达式抛出类型错误:无法读取未定义的属性“长度” - Path-to-regexp throws TypeError: Cannot read property 'length' of undefined 尝试导入错误:'path-to-regexp' 不包含默认导出(导入为 'pathToRegexp') - Attempted import error: 'path-to-regexp' does not contain a default export (imported as 'pathToRegexp') 如何处理“已弃用,请使用https://github.com/pillarjs/path-to-regexp”消息 - how to deal with “DEPRECATED use https://github.com/pillarjs/path-to-regexp” message 在 url 路径中传递多个路由参数时出错 - Error passing multiple route parameters in a url path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM