Given the following string:
var str = "[,,,1,2,,,3,4,,,,,,5,6]";
I want to replace all "empty" values with nulls. In other words, I want this result:
"[null,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6]"
This almost works, but it misses the first empty value:
var str = "[,,,1,2,,,3,4,,,,,,5,6]";
str.split(',').map(function(x) { return x ? x : 'null' }).join(',')
// Gives [,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6]
Likewise, I noticed if I have trailing empty values, it misses the last one there too:
var str = "[,,,1,2,,,3,4,,,,,,5,6,,]";
str.split(',').map(function(x) { return x ? x : 'null' }).join(',')
// Gives [,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6,null,]
Any ideas how I can make sure that first and last empty values are also replaced?
Thanks!