10

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?

4 Answers 4

16
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)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks it worked.. also can you explain to me what does /userID=(\d+)/ do?
Then how come it gets only the number 1413795052 but not userID=1413795052?
The whole matched string is returned in match[0], and all captured strings (the parts under parenthesis) are returned in match[x] where x is the number of the capture group
5

This will get all numbers in the querystring:

window.location.search.match(/[0-9]+/);

Comments

3

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

Comments

2

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] );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.