Replace all characters except numbers and commas with js
The code copy is as follows:
<script language="javascript">
var str="asdfk,asdf345345,345345";
//Replace all characters except numbers and commas.
str=str.replace(/[^0-9,]*/g,"");
//Remove the first comma
if (str.substr(0,1)==',') str=str.substr(1);
// Remove the second comma
var reg=/,$/gi;
str=str.replace(reg,"");
alert(str);
</script>
result:
The code copy is as follows:
345345,345345
Complete code:
The code copy is as follows:
FCKinsertdown.Add = function(strtemp,str) {
if(strtemp.indexOf(",")>-1){
strtemp=dostr(strtemp);
var strs= new Array(); //Define an array
strs=strtemp.split(","); //character segmentation
for (i=0;i<strs.length ;i++ ) {
if(i==strs.length){
FCK.InsertHtml("[downsoft]"+trim(strs[i])+"[/downsoft]") ;
}else{
FCK.InsertHtml("[downsoft]"+trim(strs[i])+"[/downsoft]<br />") ;
}
}
}else{
FCK.InsertHtml("[downsoft]"+dostr(strtemp)+"[/downsoft]");
}
}
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<len;i++){
var v = data[i];
if (typeof(a[v]) == 'undefined'){
a[v] = 1;
}
};
data.length=0;
for (var i in a){
data[data.length] = i;
}
return data;
}
//For users to call
function trim(s){
return trimRight(trimLeft(s));
}
//Remove the blank on the left
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 < i && whitespace.indexOf(str.charAt(j)) != -1){
j++;
}
str = str.substring(j, i);
}
return str;
}
//Remove the blank on the right
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;
}
Original articles from Wulin.com, please indicate the source when reprinting.