自动检测数字替换非数字的正则表达式
- 作者: 用户47134860
- 来源: 51数据库
- 2021-08-06
直接给出代码了:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title> new document </title>
<meta name="generator" content="editplus">
<meta name="author" content="">
<meta name="keywords" content="">
<meta name="description" content="">
</head>
<body>
<input onkeyup='check(this)'/>
</body>
<script>
function check(obj){
var sreg = /^-+.*/g;
var zero = /^0[1-9]+\.*\d*/g;
var val = obj.value;
var plus = '';
if(sreg.test(val)){
val = val.replace(/-+/g,'');
plus = '-';
}
val = val.replace(/\s+/g,'');
if(/^\.+.*$/.test(val)){
val = '';
}
val = val.replace(/[^\d\.]/,'');
val = val.replace(/(^\d+\.{1})(\d*).*/g,'$1$2');
val = val.replace(/(^\d+\.\d{3})\d*/g,'$1');
val = val.replace(/^[0]*(0{1})([1-9]*)(.*)/g,'$1$2$3');
if(zero.test(val)){
val = val.replace(/0([1-9]+)(.*)$/,'$1$2');
}
obj.value = plus+val;
}
</script>
</html>
写的一个替换 非数字与中英文逗号的函数,需要的朋友可以参考一下
function renumdou(str){
var regexp = /[^\d,,]]*/g;
newstr=str.replace(regexp,"");
return newstr
}
推荐阅读
