Regex for US phone number

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

function telephoneCheck(str) {
return /^[\+]?([0-9][\s]?|[0-9]?)([(][0-9]{3}[)][\s]?|[0-9]{3}[-\s\.]?)[0-9]{3}[-\s\.]?[0-9]{4,6}$/ig.test(str);
}

telephoneCheck("555-555-5555");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36

Challenge: Telephone Number Validator

Link to the challenge:

Yea, a tough one.
To debug it’s easier to copy paste the test specs from the left to after the function and console.log. Also it’s easier to use string .match method and console.log to see what is captured from the string using your regex. See below:

function telephoneCheck(str) {
return /^[\+]?([0-9][\s]?|[0-9]?)([(][0-9]{3}[)][\s]?|[0-9]{3}[-\s\.]?)[0-9]{3}[-\s\.]?[0-9]{4,6}$/ig.test(str);
}

console.log(telephoneCheck("2 (757) 622-7382")) // should return false.
console.log(telephoneCheck("0 (757) 622-7382")) // should return false.

let str = "2 (757) 622-7382"
let regex = /^[\+]?([0-9][\s]?|[0-9]?)([(][0-9]{3}[)][\s]?|[0-9]{3}[-\s\.]?)[0-9]{3}[-\s\.]?[0-9]{4,6}$/ig
let matched = str.match(regex);
console.log(matched)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.