-2

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?

2
  • Is that id always a number? Commented Jun 23, 2013 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. Commented Jun 23, 2013 at 2:39

3 Answers 3

2

Try this:

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

Fiddle

Sign up to request clarification or add additional context in comments.

8 Comments

hehe, you also were downvoted by some morons )
This just makes so many assumptions. The URL needs to be parsed properly.
@naomik: we are developers, we solve the tasks as they appear. The theoretics may theoretize for years, while we just provide a solutions for the business.
@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?
This won't match if the id is the only URL parameter, correct?
|
1
params = location.search.substring(location.search.indexOf('id')).split('&')[0]

  id = params.substr(3)

3 Comments

the question asked for the query params, right? what is wrong
This looks like the best answer here.
Note that this should be wrapped up into a function so that inside you can count the length of the key and dynamically populate that substr argument.
0

If it is always shipped in this exact order - then

url.match(/\d+/)

otherwise

url.match(/id=(\d+)/)

6 Comments

Sorry, but this is just asking for trouble.
@naomik: I'm following the task description. If it's not complete - it's not my problem.
@naomik: please prove it's not the best? Please keep in mind that your criterias to determine the solution quality may not match mine.
@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
The second line of code is wrong, you are missing a [1].
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.