1

How to use javascript to get the following url - the value of id?

URL: http://www.url/id/2721/John

I would get the following values:

2721 and John
Corbin
  • 32,222
  • 6
  • 66
  • 77
JohnMalcom
  • 841
  • 4
  • 16
  • 28
  • 6
    out of curiosity, what have you tried? – MilkyWayJoe May 07 '12 at 21:26
  • Show us what you've tried and why/where you're hung up. Don't just expect us to do the work for you. – James Johnson May 07 '12 at 21:30
  • Possible duplicate? http://stackoverflow.com/questions/6168260/how-to-parse-a-url http://stackoverflow.com/questions/4140324/parse-url-with-javascript http://stackoverflow.com/questions/7840306/parse-url-with-javascript-or-jquery http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript – Jack May 07 '12 at 21:32
  • http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/ – kmb64 May 07 '12 at 21:34

2 Answers2

3

Use .split("/") to separate the URL into pieces, then read the values.

Niet the Dark Absol
  • 311,322
  • 76
  • 447
  • 566
0

Use .match(re):

var r = "http://www.url/id/2721/John".match('\/id\/(\\d+)/([^/]+)');

The value v[1] will be 2721 and r[2] will be John.

kapex
  • 27,631
  • 6
  • 104
  • 117
gonz
  • 4,986
  • 4
  • 38
  • 54