Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

Given the following string/query string:

ajax/hovercard/hovercard.php?id=100000472545907&extragetparams=%7B%22hc_location%22%3A%22stream%22%7D 

What is the best way to extract the id?

share|improve this question
Is that id always a number? – Surreal Dreams Jun 23 at 2:36
1  
Any question that says "What is the best way..." to do something usually means it will be a subjective (not objective) answer. – Alex W Jun 23 at 2:39

marked as duplicate by brendan, Sushanth --, Alex W, adeneo, LittleBobbyTables Jun 23 at 2:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

If it is always shipped in this exact order - then

url.match(/\d+/)

otherwise

url.match(/id=(\d+)/)
share|improve this answer
Sorry, but this is just asking for trouble. – naomik Jun 23 at 2:34
@naomik: I'm sorry? – zerkms Jun 23 at 2:35
You're just making so many assumptions about the URL. This is a shameful recommendation from a user with a reputation as high as yours. – naomik Jun 23 at 2:37
3  
@naomik: I'm following the task description. If it's not complete - it's not my problem. – zerkms Jun 23 at 2:38
3  
@naomik: you're making assumptions that such key may appear in the url. I'm used to using APIs that don't change randomly. Stop making assumptions please. If you want to prove my solution won't work - please ask OP to provide the URL my regex won't match – zerkms Jun 23 at 2:51
show 3 more comments

Try this:

yoururl.match(/id=(.*)&/)[1]

Fiddle

share|improve this answer
3  
hehe, you also were downvoted by some morons ) – zerkms Jun 23 at 2:36
This just makes so many assumptions. The URL needs to be parsed properly. – naomik Jun 23 at 2:36
2  
@naomik: treat it as a way to teach a person about how to ask better question. Well, I see your point, thank you for being such pedantic. Give me a second to check your answer. See you there. stackoverflow.com/a/17245542/251311 --- here is the first. Should I continue? – zerkms Jun 23 at 2:39
1  
@naomik it is such a straight forward regex. What explanation is required for you. If OP needs he will ask and we will give him explanation. By the way the question is also just a one liner... There is not much to talk about it. – PSL Jun 23 at 2:44
2  
This won't match if the id is the only URL parameter, correct? – Alex W Jun 23 at 2:47
show 4 more comments
params = location.search.substring(location.search.indexOf('id')).split('&')[0]

  id = params.substr(3)
share|improve this answer
the question asked for the query params, right? what is wrong – user2404546 Jun 23 at 2:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.