RegEx Phone Number Validation

I am unsure why this regex is failing. It validates some numbers, but not all.

let regexp = /1?\s?((\(\d{3}\))|(\d{3}))(-|\s)?\d{3}(-|\s)?\d{4}/;

For example, regex.test("(6054756961)") returns true when it should be returning false. I’ve check this regex on a couple different sites and I cannot see why it’s still returning true.
Zero or one 1? True.
Zero or one space? True.
Literal ( followed by 3 digits followed by literal ), OR 3 digits? False

What’s going on here?

You are not enforcing your regex to start its matching from the start of the string. Therefore it can start from ANYWHERE.

/1?\s?((\(\d{3}\))|(\d{3}))(-|\s)?\d{3}(-|\s)?\d{4}/.test("(6054756961)") returns true as a successful match can occur if matching starts at index 1 (the "6" character).

There is a special character you can use in your regex to force it to start matching from the start of the string. There is also another you may find useful which ensures it stops at the end of the string.

Ah that makes sense. So, even though the entire string wouldn’t match, it would still return true because SOME match was found within the string, correct?

That’s right.

Consider the simpler case of /b/.test("abc"). It returns true simply because there is a "b" in "abc". Its position doesn’t matter.