2016-05-30 16 views
1

私はregexpで間違ったことに私に答えることができます。私は、これらの3つのフォーマット:phonenumberから値を渡すregexpを書く必要があります。 "+38(093)937-99-92"と "093 937 99 92"そして "(093)937 99 92"正規表現を何も繰り返さない

私はそのことを書き始めている間、私は私がエスケープすべてのシンボルエスケープ記号についての私の例では知っているexample of error and how i do

は、他のすべては、私が知っているように、正規表現の一部でエラーが...持っていた

$.validator.addMethod("phone", function(value, element) { 

     return /^\+[0-9]{2}\([0-9]{3}\)[0-9]{3}+$/i.test(value); 
    } 
+0

これは、 '+'末尾のです。 '(?:[0-9] {3})+'が必要になります。 – andlrc

+0

Rubular.comと呼ばれる素晴らしいウェブサイト/ツールがあります。ページにregexを使用するためのいくつかのドキュメントがありますが、実際の宝石は正規表現やサンプルデータを入力し、すべてを解析する方法です。このような問題にはとても便利です。 –

答えて

1

説明

^(?:\+[0-9]{2}\s)?(?:\([0-9]{3}\)|[0-9]{3})\s[0-9]{3}(-|\s)[0-9]{2}\1[0-9]{2} 

Regular expression visualization

次の形式

  • +38(093)937-99-92
  • 093 937 99 92内の文字列にマッチする。この正規表現
  • (093)937 99 92
  • 問題です

ライブデモ

https://regex101.com/r/lM8hS0/1

説明

NODE      EXPLANATION 
---------------------------------------------------------------------- 
^      the beginning of a "line" 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (optional 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
    \+      '+' 
---------------------------------------------------------------------- 
    [0-9]{2}     any character of: '0' to '9' (2 times) 
---------------------------------------------------------------------- 
    \s      whitespace (\n, \r, \t, \f, and " ") 
---------------------------------------------------------------------- 
)?      end of grouping 
---------------------------------------------------------------------- 
    (?:      group, but do not capture: 
---------------------------------------------------------------------- 
    \(      '(' 
---------------------------------------------------------------------- 
    [0-9]{3}     any character of: '0' to '9' (3 times) 
---------------------------------------------------------------------- 
    \)      ')' 
---------------------------------------------------------------------- 
    |      OR 
---------------------------------------------------------------------- 
    [0-9]{3}     any character of: '0' to '9' (3 times) 
---------------------------------------------------------------------- 
)      end of grouping 
---------------------------------------------------------------------- 
    \s      whitespace (\n, \r, \t, \f, and " ") 
---------------------------------------------------------------------- 
    [0-9]{3}     any character of: '0' to '9' (3 times) 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    -      '-' 
---------------------------------------------------------------------- 
    |      OR 
---------------------------------------------------------------------- 
    \s      whitespace (\n, \r, \t, \f, and " ") 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
    [0-9]{2}     any character of: '0' to '9' (2 times) 
---------------------------------------------------------------------- 
    \1      what was matched by capture \1 
---------------------------------------------------------------------- 
    [0-9]{2}     any character of: '0' to '9' (2 times) 
---------------------------------------------------------------------- 
0

これは末尾にある+の問題です。あなたは何かを繰り返す必要があります(例:(?:[0-9]{3})+)。しかし、この正規表現は、あなたのニーズにマッチする必要があります

/^(?:\+\d{2} \(\d{3}\) \d{3}-\d{2}-\d{2}|(?:\(\d{3}\)|\d{3}) \d{3} \d{2} \d{2})$/ 

Try it online

あなたができるように、私は、リンク先のページで(スタートと文字列の末尾)^$を除去し、またgローブフラグを追加しましたどのように動作するかを見てください。

+1

Opには次のような書式があります。違いが何であるかを説明する方が良い;-) –

関連する問題