Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

This question already has an answer here:

var url = "journey?reference=123line=A&destination=China&operator=Belbo&departure=1043&vehicle=ARC"

How can I split the string above so that I get each parameter's value??

share|improve this question

marked as duplicate by Diodeus, Ian, Cameron Tinker, senshin, Joe May 5 '14 at 21:23

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.

    
@Diodeus I would like a solution that saves them all to an array and that I can use repeatedly on lots of URLs – user3605739 May 5 '14 at 20:31
    
That is a secondary issue, once you've parsed the data and created the object. – Diodeus May 5 '14 at 20:35

You could use the split function to extract the parameter pairs. First trim the stuff before and including the ?, then split the & and after that loop though that and split the =.

var url = "journey?reference=123line=A&destination=China&operator=Belbo&departure=1043&vehicle=ARC";

var queryparams = url.split('?')[1];

var params = queryparams.split('&');

var pair = null,
    data = [];

params.forEach(function(d) {
    pair = d.split('=');
    data.push({key: pair[0], value: pair[1]});

});

See jsfiddle

share|improve this answer

to split line in JS u should use:

var location = location.href.split('&');
share|improve this answer
    
Instead of location.href, it would be easier to use location.search which will only return the question mark and everything after it. Then just chop off the first character using a substring. – tomysshadow May 5 '14 at 20:48
    
@tomysshadow yeah! It's even better! Thanks! – Nikita_Sp May 6 '14 at 12:20

Try this:

var myurl = "journey?reference=123&line=A&destination=China&operator=Belbo&departure=1043&vehicle=ARC";
var keyval = myurl.split('?')[1].split('&');
for(var x=0,y=keyval.length; x<y; x+=1)
console.log(keyval[x], keyval[x].split('=')[0], keyval[x].split('=')[1]);
share|improve this answer

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