Replace all characters except numbers and commas with js
Result:
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;
}
//Deduplicate array
function unique(data){
data = data || [];
var a = {};
len = data.length;
for (var i=0; i
if (typeof(a[v]) == 'undefined'){
a[v] }; }
return data;
}
//For users to call
function trim(s){
return trimRight(trimLeft(s));
}
//Remove the left whitespace
function trimLeft(s){
if(s == null) {
return "";
}
var whitespace = new String(" tnr");
var str = new String(s);
if (whitespace.indexOf(str.charAt(0)) != -1) {
var j=0, i = str.length;
while (j < i && whitespace.indexOf(str.charAt(j)) != -1){
j ;
}
str = str.substring(j, i);
}
return str;
}
//Remove the whitespace on the right
function trimRight(s){
if(s == null) return "";
var whitespace = new String(" tnr");
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;
}
Original article from Script House, please indicate the source when reprinting.