1

I want to use regex to get an arrayLike object from the params giving to url

e.g.

http://mysite/myPeople?one=jose&two=emily&three=john

basically what it does is this

    function interpretUrl(url){

        var reg = /\?([^&?=]*)=([^&?=]*)/g; //what am i doing wrong?

        //some logic here
        reg.exec(url)

        return {
            param: [
                one: 'jose',
                two: 'emily',
                three: 'john'
            ],
            root:
        }
    }
user2167582
  • 5,103
  • 10
  • 50
  • 95
  • That's nice. What's stopping you? – Marc B Apr 11 '14 at 16:54
  • 2
    This is answered in the [Stack Overflow Regular Expressions FAQ](http://stackoverflow.com/a/22944075/2736496), both listed under "Common Validation Tasks": [matching urls](http://stackoverflow.com/a/190405/2736496) and [matching host/port](http://stackoverflow.com/a/22697740/578411) – aliteralmind Apr 11 '14 at 16:54
  • big thanks, aliteralmind – user2167582 Apr 23 '14 at 06:44

1 Answers1

1

You can use this to get all the parameters from query string:

var re = /([^?&]+)=([^&]*)/g,
    matches = {},
    input = "http://mysite/myPeople?one=jose&two=emily&three=john";
while (match = re.exec(input.substr(input.indexOf('?')+1))) matches[match[1]] = match[2];

console.log(matches);
//=> {one: "jose", two: "emily", three: "john"}
anubhava
  • 664,788
  • 59
  • 469
  • 547