Help with parentheses in the Validate US Telephone Numbers project

I was able to get only so far with my regex. Basically, it failed at the mismatched parens, and I have been unable to figure out how to prevent this problem using regex.
To satisfy the terms of the project, I kind of cheated by checking if parens exist and that there are not an odd amount of parens (i.e., mismatched).

I would like to know how to do this the correct way.

Any help would be highly appreciated.

  **Your code so far**

function telephoneCheck(str) {

let regex = /^[1]?[-\s]?\(?(\d{3})\)?[-\s]?(\d{3})[-\s]?(\d{4})$/;	

//begin a bit of a cheat to see if there is an odd number of parens present
let regParens = /[\(\)]/g;	
let parens;
parens = str.match(regParens);
if (parens != null && parens.length%2 != 0) {
	return false
//end a bit of a cheat to see if there is an odd number of parens present

return regex.test(str);		


  **Your browser information:**

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

Challenge: Telephone Number Validator

Link to the challenge:

Not sure if FCC has introduced this to you or not but there is an OR operator for regular expressions (the pipe |) that allows you to match one thing OR another. I think you may find this handy for solving this challenge.

P.S. Your expression is a little busy. You don’t need the parens around things such as (\d{4}) because you are not trying to capture them.

I’m not sure what you mean by cheating but there are many ways to solve this problem…

I kept it simple and used regex to only test if there was a parenthesis in the string. If so, then we know that there are only two possible formats which are valid:

1(555)555-5555 // this isn’t listed as an example but there is a test case for it

You can then use basic array indices to check the position of the parenthesis.

Note: data needs to be cleaned and transformed before this step.

I’ll challenge you to make it even simpler :slight_smile: This challenge can be solved with a single return statement and a fairly simple regular expression. I think this is what @josephlevin is looking for.

Yes, I was looking for one single regex to use. I tried using the OR, but without success.

I know that within the two slashes the OR works with whole phrases, like /dog|cat/, but I couldn’t get it to work for something like:


I ended up using the extra capture parens as without them nothing would match after the first few characters (such as 1, 1-, 1 with a space).

jjplant’s method of checking for the proper position of parens in the telephone number , post sanitizing, sounds like a good idea if the regex doesn’t work. It is more robust than my “cheating” solution of just checking for mismatched parens.

I’ll have to revisit the challenge.

Thanks for your help.

I just want to reassure you that it does work, you can definitely solve this with one line of code using one regex.

It also works within parentheses.

I revisited the task and figured it out without having to resort to a non-regex type of test.

   let regex = /^[1]?[-\s]?(\d{3}|\(\d{3}\))[-\s]?(\d{3})[-\s]?(\d{4})$/;
^[1]?	->	at the beginning of the string (via use of ^), zero or one number 1, followed by
[-\s]?      ->	zero or one dash or space, followed by
(\d{3}|\(\d{3}\))     ->  3 digits OR 3 digits surrounded by parens, like so: 111 OR (111), followed by
[-\s]?      ->	zero or one dash or space, followed by	
(\d{3})	->	3 digits, followed by
[-\s]?      ->	zero or one dash or space, followed by
(\d{4})$	-> 	4 digits at the end of the string (via use of $)


The problem I think I was facing was a combination of not keeping my groupings straight with parens, as well as sometimes forgetting to put the \ in front of the d{3} while getting a bit muddled.

I’m posting the solution here as the FCC tutorials do not go into this in enough depth, IMO, and a part-by-part explanation can be really helpful.

Thanks for the good hints.

1 Like

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