简体   繁体   English

Javascript:正则表达式用于正数和负数但没有小数点

[英]Javascript: regex for positive and negative number but no decimal point

I want a regex which only accepts positive and negative numbers.我想要一个只接受正数和负数的正则表达式。 It should not include decimal point or any other characters except positive and negative numbers.它不应包含小数点或除正负数外的任何其他字符。

I have tried below regex but it accepts decimal point.我试过下面的正则表达式,但它接受小数点。

^-?\d*\.{0,1}\d+$

The regex should not allow any decimal point even it is before 0 eg 122334.0000正则表达式不应允许任何小数点,即使它在 0 之前,例如122334.0000

Thank you.谢谢你。

你可以试试下面的regx。

^-?\d+$

You can do something like the below code.您可以执行类似以下代码的操作。

^\-?[1-9]\d{0,2}$

OR或者

^[+-]?\d+$

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

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