Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
28
rated 0 times [  35] [ 7]  / answers: 1 / hits: 10775  / 11 Years ago, fri, january 31, 2014, 12:00:00

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.


More From » regex

 Answers
11
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]

[#48186] Thursday, January 30, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
domeniccolti

Total Points: 276
Total Questions: 98
Total Answers: 93

Location: India
Member since Fri, May 13, 2022
2 Years ago
domeniccolti questions
Mon, Oct 18, 21, 00:00, 3 Years ago
Thu, Oct 14, 21, 00:00, 3 Years ago
Thu, Jul 15, 21, 00:00, 3 Years ago
Sat, Oct 24, 20, 00:00, 4 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;