I wanted to make a method that i could pass a recordset (could just as easily be an general object) with format string to replace values by name. I found this other answer on SO that almost did it: https://stackoverflow.com/a/2534828/359135
function format(str, obj) {
return str.replace(/\{\s*([^}\s]+)\s*\}/g, function(m, p1, offset, string) {
return obj[p1]
})
}
but it does not quite work (i don't think): http://jsfiddle.net/9Jpkv/24/
NOTE: my recordset version would just be:
function format(str, rs) {
return str.replace(/\{\s*([^}\s]+)\s*\}/g, function(m, p1, offset, string) {
return rs(p1); //use recordset names accessors instead of object properties
})
}
I would like to be able to do this without iterating over the properties of the object (or columns of the recordset) like the above code almost manages to do. instead it loks for items between curly braces and tries to find them in teh object properties (or record set columns)
Or actually I am a idiot and the above actually does work!: http://jsfiddle.net/9Jpkv/26/
I was not passing passing in an object!