Here’s some better code:
The following mainly adds a method to remove multiple commas into one comma
function dostr(str){
str=trim(str);
var strarry=unique(str.split(","));
str=strarry.join(",");
str=str.replace(/,/ig,",");
str=str.replace(/[^0-9,]*/ig,"");
str=str.replace(new RegExp(', ',"gm"),',');
if (str.substr(0,1)==',') str=str.substr(1);
var reg=/,$/gi;
str=str.replace(reg ,"");
return str;
}