0
console.log(/\d+?\d+?\d+?-\d+?\d+?\d+?-\d+?\d+?\d+?\d+?$/.test("555-555-55539"));

Answer --> true

I was looking for false, i am validating phone numbers. e.g. 555-555-5555 is a correct response([0-9])

I am a newbie to regex, can anyone explain what i am doing wrong here?

Aki G
  • 39
  • 4

2 Answers2

0

How about this.

console.log(/\d{3}-\d{3}-\d{4}$/.test("555-555-55539"));
Sankar
  • 6,908
  • 2
  • 30
  • 53
0

You used wrong quantifiers in your regex. You made them lazy (+?), but it will still match all characters until the next character from regex is found. In case of your last quantifier (just before $) it will match all digits until the end of string is found. Hence it matches not only one digit but all of them. Same thing happens before each hyphen (555555555-5555-555555555 is valid for your regex).

Egan Wolf
  • 3,533
  • 1
  • 14
  • 29