自动检测数字替换非数字的正则表达式
程序员文章站
2022-04-16 12:01:49
直接给出代码了:
&l...
直接给出代码了:
<!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 }
上一篇: java常用正则表达式