ave ave - 1 year ago 103
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 Source
/\/posts\/(\d*)\/edit/g.exec("/posts/23/edit")
// => ["/posts/23/edit", "23"]