Given the following URLPattern:
new URLPattern('/path/example/:name/:age')
I am looking to return an array of all named groups. ['name', 'age']
- https://developer.mozilla.org/en-US/docs/Web/API/URLPattern/URLPattern
- https://github.com/pillarjs/path-to-regexp
I don't see a simple library / class to do this?
I need this to take into account all supported url paths for instance /users/:id/:tab(home|photos|bio).
One way to do this that does not satisfy /users/:id/:tab(home|photos|bio) is to feed the url back into itself like this:
const x = new URLPattern({ pathname: '/path/example/:name/:age' })
const r = x.exec('https://localhost:3000/path/example/:name/:age')
console.log(Object.keys(r.pathname.groups))
[ "name", "age" ]
Try this:
(?<=\/:)\w+/g\w+means search for one or more word character fromatozor fromAtoZor from0to9or an underscore_, then(?<=\/:)it insures that the string is preceded by a/:but it does not include the value of/:as part of the result.