Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a number out of this url in javascript regex

I have this url

http://nikerunning.nike.com/nikeplus/v2/services/app/run_list.jsp?userID=1413795052&startIndex=0&endIndex=-1&filterBy=all

I want to fetch 1413795052 number using regex in javascript, how can I achieve this?

like image 668
AabinGunz Avatar asked Sep 07 '11 08:09

AabinGunz


3 Answers

var url = 'http://nikerunning.nike.com/nikeplus/v2/services/app/run_list.jsp?userID=1413795052&startIndex=0&endIndex=-1&filterBy=all';
var match = url.match(/userID=(\d+)/)
if (match) {
    var userID = match[1];
}

This matches the value of the userID parameter in the URL.

/userID=(\d+)/ is a regex literal. How it works:

  • The / are the delimiters, like " for strings
  • userID= searches for the string userID= in url
  • (\d+) searches for one or more decimal digits and captures it (returns it)
like image 185
Arnaud Le Blanc Avatar answered Oct 17 '22 19:10

Arnaud Le Blanc


This will get all numbers in the querystring:

window.location.search.match(/[0-9]+/);
like image 5
TJHeuvel Avatar answered Oct 17 '22 19:10

TJHeuvel


try it right here in stackoverflow:

window.location.pathname.match(/questions\/(\d+)/)[1]
> "7331140"

or as an integer:

~~window.location.pathname.match(/questions\/(\d+)/)[1]
> 7331140
like image 3
omikes Avatar answered Oct 17 '22 19:10

omikes