I’m finding that my regex2 and regex4 aren’t matching as they should. I think there’s a problem when matching the parentheses but I’m not sure why.
I’ve tried escaping the parentheses with a backslash as in my code below and have also tried writing just the parentheses in without using a backslash. Does anyone know why it isn’t matching correctly?
Your code so far
function telephoneCheck(str) {
var newStr = str.replace(/\s/g, '');
var regex2 = /[\(][0-9]{3}[\)][0-9]{3}-[0-9]{4}/;
var regex4 = /1[\(][0-9]{3}[\)][0-9]{3}-[0-9]{4}/;
var testStr = str.replace(/[^0-9]/g, '');
var match2 = newStr.match(regex2);
var match4 = newStr.match(regex4);
console.log(newStr.match(regex2)) //returns null
console.log(newStr.match(regex4)) //returns null
//test case
console.log(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/107.0.0.0 Safari/537.36 Edg/107.0.1418.42
Challenge: JavaScript Algorithms and Data Structures Projects - Telephone Number Validator
Link to the challenge: