Get part of the url pathname via JavaScript regex

expression, javascript, regex

Solution

location.pathname.match(/\/article\/f\/(\d+)/)[1]

I'm trying to match /article/f/ and at least 1 digit captured by the group(note the parenthesis). If that id is the single number in your path, you can get it directly by:

location.pathname.match(/\d+/)[0]

Problem

I have following url: ``` https://www.example.com/article/f/1/test+article ``` And I need to get "1" part from the url via JavaScript (pure javascript). I know that I can get it with "location.pathname.replace()" but I'm not good with regex. UPDATE Just for clarification: "https://www.example.com/article/f/" never changes, it's constant. The only part of the url that can change is "1" (article id) and "test article" (article name). And I want to catch the article id.

Original source