Need help in Telephone Number Validator

I am not able to understand how to check for brackets (opening and closing) in regex. Can anybody help me or provide me reference of regex resource that tells about the bracket clause?

Thanks in advance!

My code so far


function telephoneCheck(str) {
let newstr = str.replace(/ /g,"");
console.log(newstr[0])
let result = (/^([1])?(\d{3})(-?)(\d{3})(-?)(\d{4}$)/).test(newstr);

console.log(result, newstr, newstr[0]);
return result;
}

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/87.0.4280.88 Safari/537.36.

Challenge: Telephone Number Validator

Link to the challenge:

Hi @agrawalparul2!

I don’t know if you are already using this but regex101 is a good resource to debug your regex solution.

Nope I wasn’t aware about this. This is super cool.

just like other special symbols you can check for them by simply putting \ in front of them. For example \( or \)

That doesn’t work because closing bracket should exist only in case of opening bracket. If we use ( or ) it passes test even when either of the brackets are missing.

Thanks I could fix it putting d3 in the bracket. Thanks so much. That was a silly thing but to find it I read so much and learnt a lot in process.

yes, i suppose you already figured out, you need to use the or operator to let the regex match 2 different cases, one with brackets and one without, but i was simply answering directly to your question, how to refer to parentheses

Yes I could fix it. Thanks so much for answering. :+1:

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