最多3位数,最多3位小数

时间:2015-06-05 08:40:47

标签: javascript regex

似乎我再次陷入了一个简单的正则表达式。

我想要的是什么:

  • 1到999之间的数字
  • 可选:逗号,标志
  • 如果输入逗号,则最小1位小数,最大3位 小数应该是presebt。
  允许


  100个
  999,0
  999,999
  999,99

     不允许的


  -1
  0
  999,
  999,9999

这是我到目前为止所做的:

^[0-9]{1,3}(?:\,[0-9]{1,3})?$

任何提示?

2 个答案:

答案 0 :(得分:6)

这个正则表达式应该有效:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" 
          xmlns:jcr="http://www.jcp.org/jcr/1.0"
          cq:actions="[_clear]"
          jcr:primaryType="cq:EditConfig">

    <cq:listeners
        jcr:primaryType="cq:EditListenersConfig"
        updatecomponentlist="function(cell, allowed, componentList){
            var firstSearchPath = cell.searchPaths[0];
            var isFooterParsys = firstSearchPath.indexOf('footer/footerPar') > -1;
            if (isFooterParsys && allowed instanceof Array) {
                while(allowed.length > 0) {
                    allowed.pop(); //Remove all currently allowed components from the parsys
                }
                allowed.push('project/components/content/your-component'); //add allowed components
            }
        };"/>
</jcr:root>

以下是解释:

^[1-9]\d{0,2}(?:,\d{1,3})?$ :它应该以1到9之间的数字开头

^[1-9]:后跟最小0,最多2位数(0-9)

\d{0,2}:后跟逗号

(?:,:如果有逗号,则应该跟一到三位

\d{1,3})?:行尾

EXAMPLE

感谢@ dev-null获取此链接:Explanation

答案 1 :(得分:5)

您可以使用此正则表达式:

/^[1-9]\d{0,2}(?:\,\d{1,3})?$/

RegEx Demo

与OP正则表达式的主要区别在于使用[1-9],它在正则表达式的其余部分之前匹配数字1到9。