I have a text field that needs to remain only text or decimal. Here is the code that I'm currently using to replace everything except numbers and a decimal point. Issue is, I can't figure out a regex that will identify everything else
我有一个文本字段,它只需要保留文本或小数。下面是我现在用的代码来替换除了数字和小数点之外的所有东西。问题是,我无法找到一个regex来标识其他所有内容
document.getElementById(target).value = newVal.replace(/\D[^\.]/g, "");
The \D works fine, but I've tried (?!.), (?!\.), [^.], [^\.]
and so on...
\ D没问题,但我试过(? !),(? ! \),(^。],[^ \。)等等……
Any suggestions for a regular expression that identifies positively with anything except a number or a decimal?
对于正则表达式,除了一个数字或一个小数之外,还有什么建议吗?
Thanks for the help
谢谢你的帮助
4 个解决方案
#1
162
Use this:
用这个:
document.getElementById(target).value = newVal.replace(/[^0-9.]/g, "");
#2
7
Removing only decimal part can be done as follows:
只去除小数部分可做如下操作:
number.replace(/(\.\d+)+/,'');
This would convert 13.6667px into 13px (leaving units px untouched).
这将把13.6667px转换成13px(单位px不变)。
#3
5
Try this:
试试这个:
document.getElementById(target).value = newVal.replace(/^\d+(\.\d{0,2})?$/, "");
. getelementbyid(目标)。值= newVal.replace(/ ^ \ d +(\ \ d { 0,2 })?美元/”、“);
#4
0
Check the link Regular Expression Demo
检查链接正则表达式演示
use the below reg exp
使用下面的reg exp。
[a-z] + [^0-9\s.]+|.(?!\d)
[a - z]+[^ 0 - 9 \]+ |。(? ! \ d)
#1
162
Use this:
用这个:
document.getElementById(target).value = newVal.replace(/[^0-9.]/g, "");
#2
7
Removing only decimal part can be done as follows:
只去除小数部分可做如下操作:
number.replace(/(\.\d+)+/,'');
This would convert 13.6667px into 13px (leaving units px untouched).
这将把13.6667px转换成13px(单位px不变)。
#3
5
Try this:
试试这个:
document.getElementById(target).value = newVal.replace(/^\d+(\.\d{0,2})?$/, "");
. getelementbyid(目标)。值= newVal.replace(/ ^ \ d +(\ \ d { 0,2 })?美元/”、“);
#4
0
Check the link Regular Expression Demo
检查链接正则表达式演示
use the below reg exp
使用下面的reg exp。
[a-z] + [^0-9\s.]+|.(?!\d)
[a - z]+[^ 0 - 9 \]+ |。(? ! \ d)