Javascript 如何为00.00创建正则表达式?

Javascript 如何为00.00创建正则表达式?,javascript,regex,Javascript,Regex,我想创建一个正则表达式来验证以下所有条件。仅允许数值[0-9]值 00.00 0.00 00.0 0.0 00 0 要匹配点两侧的1个或多个零,可以使用+运算符。因为圆点有一个特殊的含义,你必须引用它0+\.0+应该可以完成这项工作 要匹配任何数字,您可以使用\d+\.\d+ 要将其限制为最多2位,请在OP/修改问题的每个注释中使用\d{1,2}\.\d{1,2},如果您想要1或2位数字,可以选择后跟(一个句点,后跟1或2位以上),您可以使用以下正则表达式: var regex = /^\d{

我想创建一个正则表达式来验证以下所有条件。仅允许数值[0-9]值

  • 00.00
  • 0.00
  • 00.0
  • 0.0
  • 00
  • 0

要匹配点两侧的1个或多个零,可以使用
+
运算符。因为圆点有一个特殊的含义,你必须引用它<代码>0+\.0+应该可以完成这项工作

要匹配任何数字,您可以使用
\d+\.\d+


要将其限制为最多2位,请在OP/修改问题的每个注释中使用
\d{1,2}\.\d{1,2}
,如果您想要1或2位数字,可以选择后跟(一个句点,后跟1或2位以上),您可以使用以下正则表达式:

var regex = /^\d{1,2}(\.\d{1,2})?$/;
// The ( ) groups several things together sequentially.
// The ? makes it optional.

如果需要1或2位数字,后跟句点,再后跟1或2位数字:

var regex = /^\d{1,2}\.\d{1,2}$/;
// The / denotes the start and end of the regex.
// The ^ denotes the start of the string.
// The $ denotes the end of the string.
// The \d denotes the class of digit characters.
// The {1,2} denotes to match 1 to 2 occurrences of what was encountered immediately to the left.
// The \. denotes to match an actual . character; normally . by itself is a wildcard.

// happy paths
regex.test('00.00'); // true
regex.test('0.00'); // true
regex.test('00.0'); // true
regex.test('0.0'); // true
regex.test('12.34'); // true (test other digits than '0')

// first half malformed
regex.test('a0.00'); // non-digit in first half
regex.test('.00'); // missing first digit
regex.test('000.00'); // too many digits in first half

// period malformed
regex.test('0000'); // missing period
regex.test('00..00'); // extra period

// second half malformed
regex.test('00.a0'); // non-digit in second half
regex.test('00.'); // missing last digit
regex.test('00.000'); // too many digits in second half

不要忘记一开始就发布您的尝试。
^[0-9]{1,2}\.[0-9]{1,2}$
之前和之后只允许两位数字。但是您的正则表达式不能验证最大2位数@阿维纳什:谢谢你,图萨。如果条件是
00.00
0.00
00.0
0.0
0.0
,以及
00
0
@这和你的问题不一样。