不工作regexp检测-1或正整数

时间:2015-08-28 17:00:06

标签: regex angularjs

如何检测-1 o任何正整数?

我尝试过^(\-1)|(?!\-)\d+$但对我不起作用。除了-1以外,这个匹配以及小于-1 =>的所有数字。 -2,-11,-1534,......

正则表达式测试:regexr.com

在Agularjs html表单中使用的Regexp:pluker

<form name="myForm">
  <input type="text" name="myInput" ng-model="myData" ng-pattern="/^(\-1)|(?!\-)\d+$/">
  <div ng-if="myForm.myInput.$error.pattern" style="color:red;">
    Input does not match the regexp pattern!
  </div>
</form>

2 个答案:

答案 0 :(得分:1)

它不起作用的原因是因为你需要放括号。否则,|将需要以-1开头的数字或以任意数量的数字结尾,但不能同时以两个数字结尾。

^(-1|\d+)$

See it in action

答案 1 :(得分:1)

我刚试过这个,它似乎对我有用:

/^-1$|^0$|^[1-9]+[0-9]*$/

此处有采访者: http://plnkr.co/edit/UM3UL0QWImrG82XXpKuw?p=preview

相关问题