JavaScript Algorithms and Data Structures Projects - Telephone Number Validator

Cant seem to remember which regex will make the failed challenge a false boolean? Please help

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

Your code so far

function telephoneCheck(str) {
  const validated = [
    /*555-555-5555*/
    /^\d{3}-\d{3}-\d{4}$/,
    /*(555)555-5555*/
    /^\(\d{3}\)-\d{3}-\d{4}/,
    /*(555) 555-5555*/
    /^\(\d{3}\)\s\d{3}-\d{4}/,
    /*555 555 5555*/ 
    /^\d{3}\s\d{3}\s\d{4}/,
    /*5555555555, done*/
    /^\d{10}/,
    /*1 555 555 5555*/
    /^1\s\d{3}\s\d{3}\s\d{4}/,
    /*1 555-555-5555*/
    /^1 \d{3}-\d{3}-\d{4}/,
    /*1 (555) 555-5555*/
    /^1\s\(\d{3}\)\s\d{3}-\d{4}/,
    /*(555)555-5555*/
    /^\(\d{3}\)\d{3}-\d{3}/,
    /*1(555)555-5555*/
    /^1\(\d{3}\)\d{3}-\d{4}/,
    /*27576227382*/
    /\\d{11}/
  ]
  return validated.some((pattern) => pattern.test(str))
}

telephoneCheck("555-555-5555");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Telephone Number Validator

Link to the challenge:

Wow! That is a lot of regex patterns! You don’t think one pattern could have been enough?

One pattern probably would but I understood this better. It has ounces of logic.

Can I use the look ahead character?

I can only think of if statements and switch? but i’ll see whats happening. Thanks!

555-555-5555
(555)555-5555
(555) 555-5555
555 555 5555
5555555555
1 555 555 5555

/^1?(\s\d{11}\s\d)$/g

How is this?

Plug your regex pattern and strings in this online tester https://regex101.com/

1 Like

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