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
  • 33,060
  • 6
  • 68
  • 78
JohnMalcom
  • 851
  • 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
  • 320,036
  • 81
  • 464
  • 592
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
  • 28,903
  • 6
  • 107
  • 121
gonz
  • 5,226
  • 5
  • 39
  • 54