Reputation: 12347
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?
Upvotes: 10
Views: 12148
Reputation: 8543
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
Upvotes: 3
Reputation: 99919
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:
/
are the delimiters, like "
for stringsuserID=
searches for the string userID=
in url
(\d+)
searches for one or more decimal digits and captures it (returns it)Upvotes: 16
Reputation: 12618
This will get all numbers in the querystring:
window.location.search.match(/[0-9]+/);
Upvotes: 5
Reputation: 152294
Try with:
var input = "http://nikerunning.nike.com/nikeplus/v2/services/app/run_list.jsp?userID=1413795052&startIndex=0&endIndex=-1&filterBy=all";
var id = parseInt( input.match(/userID=(\d+)/)[1] );
Upvotes: 2