Regular Expressions: Restrict Possible Usernames (freeCodeCamp Challenge)

Опубликовано: 22 Март 2025
на канале: Omar Shishani
397
13

Hi! :D

This is a challenge that I struggled with, which I came back to over several days to try and figure out what I was doing wrong. One of the key things was that I was not understanding the carat (^) and the dollar sign ($) symbols, and how they relate to the regular expression. It appears that a regular expression with both ^ and $ make it so that the regular expression reads literally only the code appended to these two symbols, and nothing can be inserted in between. For example:
let userCheck = /^[a-z]\d$/;
is a regular expression that allows us to have two characters exactly: first one letter, and then one number. I was confused because I thought this regular expression would also accept something like "abarry7", but that is not the case. If we want something like that, the extra characters need to be explicitly included in the regular expression, in this case by adding a plus sign:
let userCheck = /^[a-z]+\d$/;

Good luck and happy coding! :D

#javascript #regex #coding #web #development