If you always get such a string, that is, keys and values โโin double quotes, you can add {...} to the string and parse it as JSON :
// remove trailing comma, it not valid JSON var obj = JSON.parse('{' + str.replace(/,\s*$/, '') + '}');
If not, splitting a string is also easy, assuming that , and : cannot occur in keys or values:
var obj = {}, parts = str.replace(/^\s+|,\s*$/g, '').split(','); for(var i = 0, len = parts.length; i < len; i++) { var match = parts[i].match(/^\s*"?([^":]*)"?\s*:\s*"?([^"]*)\s*$/); obj[match[1]] = match[2]; }
source share