Validate US Telephone Numbers question about RegExp

Tell us what’s happening:
Hello,
I’m trying to solve ‘Validate US Telephone Numbers’ algorithm.
My code is below

this code should return true or false
as example

telephoneCheck(“1 **555)555-5555") should return false
telephoneCheck("1
(555)555-5555") should return true.
telephoneCheck("
(555)**555-5555”) should return true.
telephoneCheck(“1 555 555 5555”) should return true.

with RegExp I’m trying to catch strictle these (555) or 555 parts
I have bolded the phone number part which I’m catching
for this purpose I’m trying this RegExp part
[\D\d{3}\D]? Dose it correct?

But it’s catching …555) or (555… with single quote also
How I’can eliminate this single quote?
Your code so far

function telephoneCheck(str) {
  var regexpr = RegExp('^1?\\s?[\\D\\d{3}\\D]?\\d+', 'g');
  var test =  regexpr.test(str);
  //str.match(reg1);
  return test;
}



telephoneCheck("1 (655)555 9555");

Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/validate-us-telephone-numbers

You might try website https://regexr.com/ to help you see what your regex selects.

Good luck!

2 Likes

Thank you very helpfull link :slight_smile: