[英]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.