2013-10-17 45 views
-1
/^\(?([1-9]{1,3})\)??([0-9]{9})$/ 

这是非常有用的数字。 我是新来的正则表达式。你能向我解释它的作用吗?我有regExp它做什么?

+2

http://www.regexper.com/#%2F%5E%5C(%3F(%5B1-9%5D%7B1%2C3%7D)%5C) %3F%3F(%5B0-9%5D%7B9%7D)%24%2F – SheetJS

回答

2
/ 
^ <-- beginning of line 
\(? <-- optional "(" 
([1-9]{1,3}) <-- 1, 2, or 3 digits (each between 1 and 9) 
\)?? <-- optional ")" (matches the first close parenthesis if multiple are present in the string) 
([0-9]{9}) <-- 9 digits (each between 0 and 9) 
$ <-- end of line 
/

这似乎符合其通过在一个区域/国家代码

+0

+1认为以图像形式呈现将给出清晰的想法,但我喜欢这样。 – Praveen

2

见前缀的电话号码这种自由间隔模式。我假设你正在使用PCRE

/^    #match the beginning of the string 
\(?    #match literal (, if exists 
(    #group 1 
    [1-9]{1,3}  #match one, two, or three digit(s). The digit must be between 1-9 
)    #end of group1 
\)??    #match literal), if exists 
(    #group 2 
    [0-9]{9}  #match 9 digits, 0-9 
)     #end of group2 
$/    #match the end of the string