Last active
January 31, 2019 22:48
-
-
Save midorikocak/f131c046aadd9e26c0633f5f2007e762 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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