I am trying to build a regular expression in javascript that checks for 3 word characters however 2 of them are are optional. So I have:
/^\w\w\w/i
what I am stumped on is how to make it that the user does not have to enter the last two letters but if they do they have to be letters
You can use this regular expression:
The quantifier
{1,3}means to repeat the preceding expression (\w) at least 1 and at most 3 times. Additionally,$marks the end of the string similar to^for the start of the string. Note that\wdoes not just contain the charactersa–zand their uppercase counterparts (so you don’t need to use the i modifier to make the expression case insensitive) but also the digits0–9and the low line character_.