ave ave - 3 months ago 23
Javascript Question

Extract substring by regex

I'm trying to extract a resource id using regex into a variable.

So far I managed to match the string, but I'm confused as to how to extract the digits into a separate value.

"/posts/23/edit".match(/\/posts\/(\d*)\/edit/g)
// => ["/posts/23/edit"]

Answer
/\/posts\/(\d*)\/edit/g.exec("/posts/23/edit")
// => ["/posts/23/edit", "23"]