javascript 数字和一位小数的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20050245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular expression for numbers and one decimal
提问by Matthew Meppiel
I can't seem to get a simple regular expression to work. Here's what I have at the moment:
我似乎无法让一个简单的正则表达式起作用。这是我目前所拥有的:
$(".Hours").on('input', function (e) {
var regex = /^\d+(\.\d{0,2})?$/g;
if (!regex.test(this.value)) {
if (!regex.test(this.value[0]))
this.value = this.value.substring(1, this.value.length);
else
this.value = this.value.substring(0, this.value.length - 1);
}
});
I need the user to be able to only enter numbers and one decimal (with only two numbers after the decimal). It's working properly now, with the exception that a user cannot start with a decimal.
我需要用户只能输入数字和一位小数(小数点后只有两个数字)。它现在工作正常,除了用户不能以小数开头。
Acceptable: 23.53 0.43 1111.43 54335.34 235.23 .53 <--- Not working
可接受:23.53 0.43 1111.43 54335.34 235.23 .53 <--- 不工作
Unacceptable: 0234.32 <--- The user can currently do this 23.453 1.343 .234.23 1.453.23
不可接受:0234.32 <--- 用户目前可以这样做 23.453 1.343 .234.23 1.453.23
Any help on this?
有什么帮助吗?
回答by Tushar Gupta - curioustushar
RegExp -
正则表达式 -
^(\d+)?([.]?\d{0,2})?$
Explanation
解释
Assert position at the beginning of the string ?^?
Match the regular expression below and capture its match into backreference number 1 ?(\d+)??
Between zero and one times, as many times as possible, giving back as needed (greedy) ???
Match a single digit 0..9 ?\d+?
Between one and unlimited times, as many times as possible, giving back as needed (greedy) ?+?
Match the regular expression below and capture its match into backreference number 2 ?([.]?\d{0,2})??
Between zero and one times, as many times as possible, giving back as needed (greedy) ???
Match the character “.” ?[.]??
Between zero and one times, as many times as possible, giving back as needed (greedy) ???
Match a single digit 0..9 ?\d{0,2}?
Between zero and 2 times, as many times as possible, giving back as needed (greedy) ?{0,2}?
Assert position at the end of the string (or before the line break at the end of the string, if any) ?$?
回答by leaf
Here is a suggestion : /^((\d|[1-9]\d+)(\.\d{1,2})?|\.\d{1,2})$/
.
这里有一个建议:/^((\d|[1-9]\d+)(\.\d{1,2})?|\.\d{1,2})$/
。
Allows : 0
, 0.00
, 100
, 100.1
, 100.10
, .1
, .10
...
允许:0
, 0.00
, 100
, 100.1
, 100.10
, .1
, .10
...
Rejects : 01
, 01.1
, 100.
, .100
, .
...
拒绝:01
, 01.1
, 100.
, .100
, .
...
回答by katosh
Would this meet your needs:
这是否满足您的需求:
var regex = /^\d+([.]?\d{0,2})?$/g;
回答by Brian Stephens
Your regex:
var regex = /^\d+(\.\d{0,2})?$/g;
你的正则表达式:
var regex = /^\d+(\.\d{0,2})?$/g;
What you need:
var regex = /^\d*(\.\d{1,2})?$/;
你需要什么:
var regex = /^\d*(\.\d{1,2})?$/;
You were requiring at least one digit before the decimal (\d+
). I have also changed it so if you include a decimal, there must be at least one digit after it.
您要求小数点 ( \d+
)前至少有一位数字。我也改变了它,所以如果你包含一个小数,它后面必须至少有一个数字。