首页 > 编程 > regex > 正文

自动检测数字替换非数字的正则表达式

2020-01-20 22:06:27
字体:
来源:转载
供稿:网友

直接给出代码了:

<!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}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表