I have the following string
Name=(Last, First), Age=(31 year, 6 months, 3 day), Height= 6.1 ft, Employment=None, Email Address =/NA/, Mobile=XXXX
and I would like to split them into the following to build a dictionary
Name: "(Last, First)"
Age: "(31 year, 6 months, 3 day)"
Height: " 6.1 ft"
...
I came across this post and tried to tweak it but can get it work with keys with/out "()". Here is the code or from this link. Would appreciate your help and feel free to suggest easier or alternative way.
txt="Name=(Last, First), Age=(31 year, 6 months, 3 day), Height= 6.1 ft, Employment=None, Email Address =/NA/, Mobile=XXXX"
//var r = /.+?=.+?(?=(\([^)]+\),)|(.=)|$)/g;
//var r = /.+?=.+?(?=(=(.*),)|$)/g;
var r = /.+?=.+?(?=(\),)|(\d\b,)|$)/g;
var obj = txt.match(r).reduce(function(obj, match) {
var keyValue = match.split("=");
obj[keyValue[0].replace(/,\s/g, '')] = keyValue[1];
return obj;
}, {});
console.log(obj);