Here is my regex: (^1{1})(\s)?((|\s|-)+(\d{3})()|\s|-)+(\d{3})(-|\s)?(\d{4})
Here are the cases that are failing:
When the #user-input element contains 5555555555 and the #check-btn element is clicked, the #results-div element should contain the text “Valid US number: 5555555555”.
When the #user-input element contains 555-555-5555 and the #check-btn element is clicked, the #results-div element should contain the text “Valid US number: 555-555-5555”.
When the #user-input element contains (555)555-5555 and the #check-btn element is clicked, the #results-div element should contain the text “Valid US number: (555)555-5555”.
When the #user-input element contains 1 555)555-5555 and the #check-btn element is clicked, the #results-div element should contain the text “Invalid US number: 1 555)555-5555”
Everything else in the code works , and is fine. I just need some tips on how to get these cases to work. I’m kind of stumped on the single parentheses one, especially. Any tips are appreciated. Thanks!
Teller is right, I completed this two weeks ago and you should maybe split your regex up and then re-join using: new RegExp(reg1.source + reg2.source…etc). It might help isolate problems for you. Also, you are on track for using “or” for the brackets but you should maybe think more about those two specific options either side of “or”.
This is my first time using the forum so I don’t know what is too much to say when trying to help.
Read your regex in plain English to yourself, It helps a lot. The first capture group includes the start “^” of regex string and exactly one occurrence of “1”???
Thanks for all of your tips. I did try splitting it, but had no luck. I’ll try again, and rejoin it. I do generally understand what I’ve put in the regex, and trying to read it does help. I’m going to continue to think about it, try what you suggested.