Telephone Number Validator challenge question

Hi everyone,

My question is related to the following challenge:

According to the website, one of the possible solutions would be:

function telephoneCheck(str) {
   var regex = /^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{4}$/;
   return regex.test(str);

I’d like to know why the following extracted part is wrapped around parentheses, since they seem unnecessary to me:


What are those parentheses doing, exactly?


That weird string of brackets and slashes are known as regular expression(regex). They are typically used to search for a pattern in a string(find or replace string functions) or restrict certain characters in an input(Like for example in your function above, to restrict certain characters from being input).

You can check out this website here to check what that regex in your function is doing. Just copy and paste the regex into the website.

it is isolating the alternatives, so it is clear which parts are to be confronted with the | OR operator
so you have “3 numbers wrapped in parenthesis OR 3 numbers”

1 Like

Thank you very much! It’s clear now.