Optional javascript regex characters

I am trying to create a regular expression in javascript that checks for the presence of three word characters, however 2 of them are optional. Therefore, I have:

/^\w\w\w/i

what I'm puzzled is to make sure that the user does not have to enter the last two letters, but if they do, they should be letters

+3
source share
2 answers

You can use this regex:

/^\w{1,3}$/i

{1,3} (\w) 1 3 . , $ , ^ . , \w a - z ( i, ), 0 - 9 _.

+10

:

/^\w\w?\w?$/i

? .

$ .
$ a12, . $ .

+5

Source: https://habr.com/ru/post/1744052/


All Articles