用js替换除数字与逗号以外的所有字符的代码
人气:0用js替换除数字与逗号以外的所有字符
<script language="javascript">
var str="asdfk,asdf345345,345345";
//替换除数字与逗号以外的所有字符。
str=str.replace(/[^0-9,]*/g,"");
//去掉第一个逗号
if (str.substr(0,1)==',') str=str.substr(1);
//去掉第二个逗号
var reg=/,$/gi;
str=str.replace(reg,"");
alert(str);
</script>
结果:
345345,345345
完整代码:
FCKinsertdown.Add = function(strtemp,str) {
if(strtemp.indexOf(",")>-1){
strtemp=dostr(strtemp);
var strs= new Array(); //定义一数组
strs=strtemp.split(","); //字符分割
for (i=0;i<strs.length ;i++ ) {
if(i==strs.length){
FCK.InsertHtml("[downsoft]"+trim(strs[i])+"[https://img.qb5200.com/download-x/downsoft]") ;
}else{
FCK.InsertHtml("[downsoft]"+trim(strs[i])+"[https://img.qb5200.com/download-x/downsoft]<br />") ;
}
}
}else{
FCK.InsertHtml("[downsoft]"+dostr(strtemp)+"[https://img.qb5200.com/download-x/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;
}
//去重复数组
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;
}
//供使用者调用
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 < i && whitespace.indexOf(str.charAt(j)) != -1){
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;
}
原创文章,转载请注明出处。
加载全部内容