简体   繁体   English

JavaScript正则表达式尾随零

[英]javascript regex trailing zeros

Javascript regex Javascript正则表达式

I have following regex, i want to un-match zeros after decimal, 1.0 or 1.00 should not match but should match 1.25 or 1.2 我有以下正则表达式,我想取消匹配十进制后的零,1.0或1.00应该不匹配,但应该匹配1.25或1.2

^(?!0\.)(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d{1,2})?$

it accepts following inputs 它接受以下输入

1.00
1.25
does not accept leading zero ex. 0.25
100
1000
1,000
100,000

You need to add a negative lookahead at the end: 您需要在末尾添加负前瞻:

^(?!0\.)(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.(?!0+$)\d{1,2})?$
                                       ^^^^^^^

See the regex demo 正则表达式演示

The (?!0+$) negative lookahead restricts the subsequent consuming subpattern \\d{1,2})?$ so that the \\d{1,2} cannot match 00 or 0 any longer. (?!0+$)负向超前限制了随后的消耗子模式\\d{1,2})?$因此\\d{1,2}再也不能匹配000

这是从相反的角度看待它是允许的,而不是排除的:

^[1-9],?[0-9]*.?[1-9]*$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM