In the telephone number validator challenge on freecodecamp. i have passed all the test except these two here:
They should return false
telephoneCheck(“1 555)555-5555”) should return false.
telephoneCheck("(555-555-5555") should return false.
The code
function telephoneCheck(str) {
var regex= /^[1]*[ |\(]*\d{3}[-]*[\)]*[ ]*\d{3}[ |-]*\d{4}$/g
return regex.test(str)
}
console.log(telephoneCheck("1 555)555-5555"));
The problem is i cant configure regex to either accept both opening and closing brackets or return false.
Link To Problem:Telephone regex challenge
Hey Kamal,
Try using regex groups: ()
And the regex OR: |
If you don’t know it, this site is handy for fiddling with regex: https://regexr.com/
(It has also cheatsheet instructions for reference)
Add something like this, to see all results:
(activate multiline-flag in upper right corner)
should return true.
1 555-555-5555
1 (555) 555-5555
5555555555
555-555-5555
(555)555-5555
1(555)555-5555
1 555 555 5555
1 456 789 4444
Should return false:
555-5555
5555555
1 555)555-5555
123**&!!asdf#
55555555
(6054756961)
2 (757) 622-7382
0 (757) 622-7382
-1 (757) 622-7382
2 757 622-7382
10 (757) 622-7382
27576227382
(275)76227382
2(757)6227382
2(757)622-7382
555)-555-5555
(555-555-5555
(555)5(55?)-5555
Hey Brother, can you tweak my code to show where i needed to make the changes i understood the problem but, am still not getting .
I am already using OR |.
Shed some light , thanks for reading.
So currently the ones in blue are getting matched but shouldn’t:
In the first case you want either of these two combinations together:
"space"555"space"
OR
(555)
I emphasize together, because in your code you have an OR between the first space and (
. That’s how you end up with "space"555)
being matched, even though its asymmetric.
Case 2:
You want this together:
(555)
OR
"nothing"555"nothing"
Again your first OR causes trouble, because it isn’t linked to a closing )
or a second "nothing"
I hope this created a little shimmer and not more darkness
PS: Try to use regex groups as well -> ()