JavaScript regex | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

JavaScript regex

Please help, What would I have to add to this code below to allow the option for zip codes with 6 digits? function isUSAZipCode(str) { return /^\d{5}(-\d{4})?$/.test(str); }

18th Oct 2021, 11:06 AM
eMBee
eMBee - avatar
12 Answers
+ 2
return /^\d{5,6}(-\d{4})?$/.test(str); ?
18th Oct 2021, 11:36 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 2
/(^\d{5}$)|(^\d{5}-\d{4}$)/
18th Oct 2021, 11:13 AM
Pariket Thakur
Pariket Thakur - avatar
+ 2
/^\d{6}(-\d{4})?$/ Simple replace 5 with 6 now it will allow 6 digit ex : 231301-2123 //pass 618464 //Pass if u want both u can use 5 || 6 or 5,6 /^\d{5||6}(-\d{4})?$/ /^\d{5,6}(-\d{4})?$/
18th Oct 2021, 11:36 AM
Pariket Thakur
Pariket Thakur - avatar
+ 2
It's one of the concept taught in Sololearn itself. You can use {A,B} to specify the repeation range of a sub regex. I would like to memorize it as {N} is derived from the syntax. https://www.sololearn.com/learn/9639/?ref=app
18th Oct 2021, 11:42 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
https://regexr.com This might help u
18th Oct 2021, 11:42 AM
Pariket Thakur
Pariket Thakur - avatar
+ 1
Thanks once again Beauty and CarrieForle You both are my icons With people like you both on the SoloLearn community, I think you guys are making SoloLearn a much faster option for getting fast and reliable answers than stackoverflow
18th Oct 2021, 11:56 AM
eMBee
eMBee - avatar
0
Beauty your Code doesn't work
18th Oct 2021, 11:20 AM
eMBee
eMBee - avatar
0
Beauty your code still doesn't allow the option for 6 digits, only 5
18th Oct 2021, 11:21 AM
eMBee
eMBee - avatar
0
Beauty, please checkout this website, you'll see that the code is still not working https://www.javascript-coder.com/form-validation/javascript-zip-code-validation/
18th Oct 2021, 11:29 AM
eMBee
eMBee - avatar
0
You got it CarrieForle Can you please explain to me your code or send me an article or a link that you used to learn regex?
18th Oct 2021, 11:40 AM
eMBee
eMBee - avatar
0
Beauty thanks
18th Oct 2021, 11:43 AM
eMBee
eMBee - avatar