I have the following URL:
http://example.com/product/1/something/another-thing
Although it can also be:
http://test.example.com/product/1/something/another-thing
or
http://completelydifferentdomain.tdl/product/1/something/another-thing
And I want to get the number 1 (id) from the URL using Javascript.
The only thing that would always be the same is /product. But I have some other pages where there is also /product in the url just not at the start of the path.
What would the regex look like?
Use
window.location.pathnametoretrieve the current path (excluding
TLD).
Use the JavaScript string
matchmethod.Use the regex
/^\/product\/(\d+)/to find a path which starts with /product/, then one or more digits (addiright at the end to support case insensitivity).Come up with something like this: