Issue with Regex in Telephone Number Validator project

Hello,

First time poster here!

I’m currently doing the JavaScript Algorithms and Data Structures Projects: Telephone Number Validator.
I’m trying to use a regex to check the format of the phone number, but there’s obviously an issue with it.

This is what I have right now:
const regex = /(1\s?)(\(?)(\d{3})(\)?)(\s?)(-?)(\d{3})(\s?)(-?)(\d{4})/g;

(Basically I’m checking for:

  • A leading 1 followed by a space, non-mandatory
  • An opening parenthesis, non mandatory
  • 3 digits followed by non-mandatory closing parenthesis, space, dash
  • 3 digits followed by non-mandatory space, fash
  • 4 digits)

I’ve checked it in regexr.com, and it appears to work as planned… but it doesn’t work in FCC. The regex appears entirely in dark red. I’m a bit lost for leads…

Thanks a lot!

Edited –

Broswer Chrome
Link to challenge

could you post a link to your code so i can take a look?

Sure, here it is:

function telephoneCheck(str) {
    // make sure that the string is 10 or 11 number long. If 11 numbers, the first must be a 1

if (str.length < 10 || str.length > 11) { 
  return false
}

//reject if letters in the string 
const regexLetter = /[a-zA-Z]+/g;
const foundLetter = regexLetter.test(str);
console.log('foundLetter for ' + str + '= '+foundLetter);
if (foundLetter == true) {
  return false
}

// make sure the string contains only numbers () spaces and dashes
const regex = /[1\s?](\(?)(\d{3})(\)?)(\s?)(-?)(\d{3})(\s?)(-?)(\d{4})/g;
const found = regex.test(str);
console.log('found for '+ str + '= '+found); 
 
  return true;
}

telephoneCheck("555-555-5555");

I am failing some of the tests, and I can’t see the commonalities between the tests I fail, so I think it has to do with the regex :confused:

thanks!