●Home
●Search
●Previous
●Next
Just sharing a nice little code snippet that makes use of regular expressions instead of loops for converting.
MY EBOOK£5 for Working the Command Line
Gain command-line shortcuts and processing techniques, install new tools and diagnose problems, and fully customize your terminal for a better, more powerful workflow.
£5 to own it today
function getQuery(s) {
var query = {};
s.replace(/\b([^&=]*)=([^&=]*)\b/g, function (m, a, d) {
if (typeof query[a] != 'undefined') {
query[a] += ',' + d;
} else {
query[a] = d;
}
});
return query;
}
// usage:
// var o = getQuery('maps.google.co.uk/maps?f=q&q=brighton&ie=UTF8&iwloc=addr');
// console.dir(o);
Note that if a query string key is found more than once, it's value is appended. If you don't want this functionality, remove the test for undefined.
Published under #code & #code & #regex. Edit this post
Archives
●All years
●2026#2
●2025#22
●2024#15
●2023#21
●2022#16
●2021#29
●2020#37
●2019#59
●2018#26
●2017#36
●2016#61
●2015#33
●2014#26
●2013#21
●2012#12
●2011#15
●2010#24
●2009#25
●2008#60
●2007#85
●2006#40
Links
●About Remy
●Work with Remy
●Speaking & Training
●Links
●Things I Fixed
●Things I Learned
●Books I've Read
●Ethos
●House Rules
●The Attic
●On GitHub
●On Twitter
●RSS feeds
●Search
I'm a JavaScript developer working professionally on the web since 1999. I run my own consultancy, build products, run training, speak at conferences and curate the UK's best JavaScript conference. You can hire me too.
License
All content by Remy Sharp and under creative commons and code under MIT license.
All code and content for this blog is available as open source on GitHub.