So, the challenge is that we are trying to detect if a string matches a fixed phone number pattern, this is a simple string pattern.
The pattern is:
Declaring a regex object pre-compiles it for any future use. Since you are looping through several test strings, it should perform better to instantiate the object outside the function first:
var rex = /^\d{3}-\d{3}-\d{4}$/;
Then the function would be:
function matchesPattern(pattern) {
return rex.test(pattern);
}