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 regexr.com, I modified it to the following and still not able to figure it out. Any suggestions?

[1]?\s?(?\d{3,4}[-.)]?\s?\d{3}[-.\s]?\s?\d{4}(?!))

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);
}


telephoneCheck("555-555-5555");
**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:
https://www.freecodecamp.org/challenges/validate-us-telephone-numbers

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:
https://regexr.com/

Quick table of regex operators:
http://tldp.org/L…/Bash-Beginners-Guide/html/sect_04_01.html

In-depth tutorial, looks very granularly at the regex engine, uses colors to illustrate concepts:
https://www.regular-expressions.info/tutorial.html

2 Likes

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