我想要一种方法来匹配尽可能多的URL参数的例子
/teams/:teamID/players/:playerID/seasons/:seasonID/detail将匹配任何以/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和不匹配
/Giants/123/players/456/seasons/2020/detail/12345我使用的是path-to-regexp。
发布于 2020-04-23 04:22:15
我发现了一个很好的库,它完全满足了我的需求,非常轻便,并且使用经过良好测试的Backbone.js片段来进行模式匹配:
https://github.com/HenrikJoreteg/feather-route-matcher
示例:
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'}这提供了我想要和需要的东西
发布于 2020-04-21 09:59:49
如果有可能不使用正则表达式,您可能可以使用/作为分隔符将URL拆分为一个数组。获取数组中在team之后一直到details的每个元素。
https://stackoverflow.com/questions/61334749
复制相似问题