2013-12-11 64 views
0

我需要一个正则表达式匹配喜欢的句子:正则表达式条件与jQuery

1:99.99.99

2:99.99.99,99

逗号是可选的,如果给它必须有1位或2位之后。我有这样的正则表达式,其中第一句匹配,但不匹配第二:

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

回答

1

随机$试试这个

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

更好的EXP:

(,[0-9]{1,2})? 

[0-9]{1,2}高达限2
(,?[0-9]{1,2})?这整个群体的任何数字高达限1

+0

不会允许他们在没有逗号结尾添加1个或2个额外的号码? – M21B8

+0

@ M21B8你是对的,我仍然错过了这一点。让我纠正它。 – Praveen

1

试试这个:

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

(未经测试,写了我的头顶部)

编辑:曾在中间

1

您可以使用以下。

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

Live demo

正则表达式:

^    the beginning of the string 
(?:    group, but do not capture (2 times): 
[0-9]{1,2}  any character of: '0' to '9' (between 1 and 2 times) 
\.    '.' 
){2}   end of grouping 
[0-9]{1,2}  any character of: '0' to '9' (between 1 and 2 times) 
(?:   group, but do not capture (optional) 
    ,   ',' 
    [0-9]{1,2} any character of: '0' to '9' (between 1 and 2 times) 
)?    end of grouping 
$    before an optional \n, and the end of the string