Regex not yielding correct answer in Challenge Validate US Telephone numbers

Below is my attemp at solving this challenge:

function telephoneCheck(str) {
  var regex = /^(1\s)?(\d{3} | [(]\d{3}[)])[-\s\S]\d{3}[-\s\S]\d{4}/;
  // Good luck!
  return regex.test(str);
  
}

telephoneCheck('1 555-555-5555');

Could someone please point out the flaw in my regex?

For one, note that regex doesn’t ignore spaces, like the spaces around the pipe character in your regex.

Thanks. Missed that one.

Now, I’ve modified my regex to:

var regex = /^(1(\s)?)?(\d{3}|[(]\d{3}[)])([-\s])?\d{3}([-\s])?\d{4}/

It satisfies all test cases except telephoneCheck(“27576227382”) and telephoneCheck("(275)76227382"). I’m guessing it has to do something with the starting ‘1’ condition. But I don’t understand how the test is yielding true in the above two cases.

Also, as you can notice I made the [-\s] optional to account for the non-whitespace as well but why didn’t it work with \S?

It matches every digit except the last one.
https://repl.it/MPcc
The way I understand it, a phone number can only have 11 digits when it starts with 1.

\S (a character that’s not whitespace) is the opposite of \s (a character that’s whitespace), and by putting them together in brackets ([\s\S]), you’re basically testing if it matches any character.

Thanks for pointing that out. I didn’t take into account that pairing a character class with its inverse will cancel out the intended effect.

As for testing digits beginning with 1, this is what I intended - to make the initial 1 optional, so that if a digit begins with any other number, the total 11 digits would be tested with the condition meant for the remaining 10 and, therefore the test would return false.
But I don’t understand why it returns true.

Have you tried testing for the end of the string?

Oh of course! Thank you so much. I added a ‘$’ at the end of the regex and it works now.