Validate US Telephone Numbers - regex

Tell us what’s happening:
I’m not sure why my regex string isn’t working to correctly pass the test. Any suggestions on what I’m missing

***after working on the older version in, I modified it to the following and still not able to figure it out. Any suggestions?


Your code so far

	var regex = /^1?\s?\((?=\d{3}\))\d{3}[-.)]?\s?\d{3}[-.]?\s?\d{4}(?!\))(?!\d+)/;
  // Good luck!
  return regex.test(str);

**Your browser information:**

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

Link to the challenge:

Ah, regex: I found these three resources very helpful (especially regexr)

Testing site, much like codepen or jsfiddle - uses coloring and explains step-by-step what is going on in what you write:

Quick table of regex operators:…/Bash-Beginners-Guide/html/sect_04_01.html

In-depth tutorial, looks very granularly at the regex engine, uses colors to illustrate concepts:


Thanks @vipatron - that is helping a ton! Especially the regexr site!

1 Like

My process for writing regular expressions.

  1. Try writing it.
  2. Fail.
  3. Open regexr.
  4. Succeed.

I’m getting closer - still having issues, but making progress.

1 Like

I remember that for that particular challenge, I copy/pasted the various number formats into the lower textarea on regexr, and had to read the test cases below the “run tests” buttons a few different times. That, and a lot of console.log statements. It was one of the harder ones. Keep it up!

1 Like