2

I want to create a regexp for phone number in jQuery. My code:

var name_regexp = /\(\+?([0-9]{2})\)?([ .-]?)([0-9]{3})\2([0-9]{3})\2([0-9]{1,3})/;

  if (($phone).match(name_regexp))
  /*do sth */

And I use this expression because I want to support sth like that (+11)111111111, but I am trying to support:

  • 555333222
  • 555 333 222
  • (11)555333222
  • (11)555 333 222
  • (+11)555333222

However, when I change expression to

var name_regexp = /\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/;

I get no matches.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Roberto
  • 315
  • 1
  • 5
  • 18
  • Double backslashes in the RegExp constructor notation to escape special characters. Nothing to repeat is related to `(+?` regex part at the start. – Wiktor Stribiżew Jul 18 '16 at 11:16
  • Please look again on my post. The second error is not related - thsi is a correct answer from http://stackoverflow.com/questions/6960596/example-of-a-regular-expression-in-jquery-for-phone-numbers – Roberto Jul 18 '16 at 11:35
  • I did not change, but RegExp is part of JavaScript, not JQuery. – Wiktor Stribiżew Jul 18 '16 at 11:52
  • just use "new RegExp" which is belong to jQuery thats why this is in title – Roberto Jul 18 '16 at 11:52
  • I think you need `var name_regexp = /^(?:\(\+?[0-9]{2}\))?[ .-]?[0-9]{3}([ .-]?)[0-9]{3}\1[0-9]{1,3}$/`, right? – Wiktor Stribiżew Jul 18 '16 at 11:56
  • Yes and I change code to work it, just wrote match(/^(?:\(\+?[0-9]{2}\))?[ .-]?[0-9]{3}([ .-]?)[0-9]{1,3}\1[0-9]{1,3}$/)) without variables. TY for help. Please add this answer. – Roberto Jul 18 '16 at 12:01
  • Before I can reopen the question, you should re-word it since the original question sounds as a duplicate. The real issue is that the `var name_regexp = /\(\+?([0-9]{2})\)?([ .-]?)([0-9]{3})\2([0-9]{3})\2([0-9]{1,3})/;` is not matching all your test cases. – Wiktor Stribiżew Jul 18 '16 at 12:04
  • Try this one: (^\((\+?\d{2})\)(\d{3} ?){3}$|^(\d{3} ?){3}$) – Michał M Jul 18 '16 at 12:48

1 Answers1

1

To match the numbers you supplied, you need to actually match one or zero spaces, dots or hyphens right after the area code inside parentheses, and only capture and back reference the delimiter after the first 3 digits:

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

See the regex demo.

You also need to allow an optional + at the beginning with \+?, 2 digits inside parentheses, and the whole prefix should be made optional (=put inside an optional group with (?:...)?).

Pattern details:

  • ^ - start of string
  • (?:\(\+?[0-9]{2}\))? - 1 or 0 occurrences of:
    • \( - one (
    • \+? - one or zero +
    • [0-9]{2} - 2 digits
    • \) - a closing )
  • [ .-]? - 1 or 0 spaces, dots or hyphens
  • [0-9]{3} - 3 digits
  • ([ .-]?) - Group 1 capturing the delimiter (a space, dot or hyphen)
  • [0-9]{3} - 3 digits
  • \1 - the same delimiter captured into Group 1
  • [0-9]{1,3} - 1 to 3 digits
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563