3

I only want to have the numbers of my URL as return. At the moment I use:

alert(document.URL);

to get the whole URL. But is there any other easy solution to get only the ID-Numbers from my URL as result? I also use jQuery and PHP in this project.

Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
Yannic Hansen
  • 235
  • 3
  • 17

2 Answers2

3

var numbers = document.URL.match(/\d+/g) will return all numbers in a URL (eg for this thread) document.URL.match(/\d+/g) => ["19570840"]

megawac
  • 10,953
  • 5
  • 40
  • 61
  • Thats exactly what i searched for! Thanks alot! – Yannic Hansen Oct 24 '13 at 22:00
  • But can you tell me what this (/\d#/g) stands for? – Yannic Hansen Oct 25 '13 at 07:08
  • 1
    @YannicHansen `\d` will match a number, `#` is a symbol, and the `/g` is the global flag. So together that regex will match all the first numbers followed by a pound symbol. eg `"test1321#a and #1 and 313#a".match(/\d#/g);` => `["1#", "3#"]` – megawac Oct 25 '13 at 14:16
2
alert(document.URL.match(/\d+/g));

Simple. I would put it in a fiddle, but uhh, jsFiddle doesn't have any numbers in their url, so.

Jace Cotton
  • 2,004
  • 1
  • 21
  • 38