-3

I am trying to write a regular expression that will require 3 characters or empty.

I start with ^[A-Z]{3}$ but I have no idea how to end it.

1 Answers1

2

You may use an alternation here:

^(?:[A-Z]{3}|)$

This pattern says to match:

^             from the start of the input
(?:
    [A-Z]{3}  3 uppercase letters
    |         OR
              (empty)
)$            end of the input
Tim Biegeleisen
  • 451,927
  • 24
  • 239
  • 318