用js替换除数字与逗号以外的所有字符
结果:
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;
}
//去重复数组
function unique(data){
data = data || [];
var a = {};
len = data.length;
for (var i=0; i
if (typeof(a[v]) == 'undefined'){
a[v] = 1;
}
};
data.length=0;
for (var i in a){
data[data.length] = i;
}
return data;
}
//供使用者调用
function trim(s){
return trimRight(trimLeft(s));
}
//去掉左边的空白
function trimLeft(s){
if(s == null) {
return "";
}
var whitespace = new String(" \t\n\r");
var str = new String(s);
if (whitespace.indexOf(str.charAt(0)) != -1) {
var j=0, i = str.length;
while (j j++;
}
str = str.substring(j, i);
}
return str;
}
//去掉右边的空白
function trimRight(s){
if(s == null) return "";
var whitespace = new String(" \t\n\r");
var str = new String(s);
if (whitespace.indexOf(str.charAt(str.length-1)) != -1){
var i = str.length - 1;
while (i >= 0 && whitespace.indexOf(str.charAt(i)) != -1){
i--;
}
str = str.substring(0, i+1);
}
return str;
}