Parentheses phone number regex problem

Tell us what’s happening:
I have been trying to follow the Hint, for this challenge and make this by steps.
While I was trying to set the boolean value, for “hasCorrectParentheses” with following regex:

(First I filtered the string to contain only parentheses and digits. and then:)

let hasCorrectParentheses = /^(\(\d{3}\)\d{4})|(\d\(\d{3}\)\d{4})|\d{10,11}$/.test(stringWithOnlyDigitsOrParentheses);

Unfortunatelly it catches also the string:

"(555-555-5555"

as with correct parentheses and I have no idea why.

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36.

Challenge: Telephone Number Validator

Link to the challenge:

  • List item

I’m not sure I understand. When I put that regex string into a regex playground, I find that it this doesn’t work the way you think. For example,
(\(\d{3}\)\d{4}) matches (123)1234. Is that a phone number? Is that what you want to match?

Similarly, the second group, (\d\(\d{3}\)\d{4}) matches 1(123)1234 and the last goup only matches 10-11 consecutive numbers.

I don’t think that regex is doing what you think it is.

On one hand you are right, I should have put 3 more \d signs in the first and second groups. But AFAICS this doesn’t change that much. After change:

/^(\(\d{3}\)\d{7})|(\d\(\d{3}\)\d{7})|\d{10,11}$/

will catch “(1234567890” (10 digits) as true, while it is not a valid string (as you see there is only one parentheses sign.

So basically I want this string above to catch:
“5(555)5555555” (11 signs with parentheses), “(555)5555555”(10 signs with parentheses), “55555555555” (11 signs without parentheses), “5555555555” (10 without parentheses).

And NOT catch (any) number of digits that if have parenetheses don’t have them on the proper place like: “(5555555555” or “55555(555)55”.

So to put it simply, I’m trying to figure out why this regex of my is matching “(5555555555”

EDIT…
I’ve just figured it out:

The $ sign was relating to just last group, so it was saying, “OR ends with 10/11 digits” which was true.

The correct regex would be:

/^(\(\d{3}\)$\d{7})|^(\d\(\d{3}\)\d{7})$|^\d{10,11}$/

(^$ on every group)