Skip to content

Instantly share code, notes, and snippets.

@midorikocak
Last active January 31, 2019 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save midorikocak/f131c046aadd9e26c0633f5f2007e762 to your computer and use it in GitHub Desktop.
Save midorikocak/f131c046aadd9e26c0633f5f2007e762 to your computer and use it in GitHub Desktop.
function myFunction() {
var str = "http://www.wikipedia.org/default.html?ct=32&op=92&item=98";
var patt = /[?&](?<key>[^&]*)=(?<value>[^&]*)/giu;
let result = str.match(patt);
let extra = result.map(item=>item.match(/([^&?=]+)/g))
var object = extra.reduce(function(o, val) { o[val[0]] = val[1]; return o; }, {});
console.log(object)
}
// with simple string methods
function withSimple(){
var address = "https://en.wikipedia.org/default.html?ct=32&op=92&item=98"
var queryStart = address.indexOf("?")
var queries = address.substr(queryStart+1).split("&")
var keyValueArray= queries.map(item=>item.split("="))
var object = keyValueArray.reduce(function(o, val) { o[val[0]] = val[1]; return o; }, {});
console.log(object);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment