'how to extract url id from string with regex?

suppose that, i've this string:

google.com/:id/:category

how can i extract only id and category from this string?

i should use regex

this match doesn't work:

match(/\/:([a-zA-Z0-9]*)/g);



Solution 1:[1]

Well, capture groups are ignored in match with /g. You might go with matchAll like this:

const url = "google.com/:id/:category"
const info = [...url.matchAll(/\/:([a-zA-Z0-9]*)/g)].map(match => match[1])
console.log(info)

Credit: Better access to capturing groups (than String.prototype.match())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1