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?

1
  • /^\d{3}-\d{3}-\d{4}$/ Commented Aug 30, 2017 at 6:36

2 Answers 2

0

How about this.

console.log(/\d{3}-\d{3}-\d{4}$/.test("555-555-55539"));

Sign up to request clarification or add additional context in comments.

Comments

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).

1 Comment

Thanks for the explanation Egan!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.