It’s working but I’d like to improve it:
**My code so far**
function telephoneCheck(str) {
const validatorRegex = /^((1\s){0,1}\d{3}-\d{3}-\d{4}|((1\s){0,1}|1)\(\d{3}\)\s{0,1}\d{3}-\d{4}|(1\s){0,1}\d{3}\s\d{3}\s\d{4}|\d{10})$/
// console.log(validatorRegex.test(str))
return validatorRegex.test(str);
}
//true: as expected
telephoneCheck("555-555-5555");
telephoneCheck("1 555-555-5555");
telephoneCheck("1 (555) 555-5555");
telephoneCheck("1(555)555-5555");
telephoneCheck("(555)555-5555");
telephoneCheck("1 555 555 5555");
telephoneCheck("555 555 5555");
telephoneCheck("5555555555");
//false: as expected
telephoneCheck("27576227382");
telephoneCheck("1 555)555-5555");
telephoneCheck("555-5555");
telephoneCheck("5555555");
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.105 Safari/537.36
.
Challenge: Telephone Number Validator
Link to the challenge: