0

I have the following regex pattern:

^[A-Za-z][A-Za-z0-9_-]+$` 

It is used to match; alphanumeric characters, underscores and dashes, with the first character being alphabetical.

This works as expected, but I also need it to be able to match single characters. A conditions of a fails.

How can I modify the pattern to make a single alphabetical character pass?

S-K'
  • 3,188
  • 8
  • 37
  • 50

2 Answers2

4
^[A-Za-z][A-Za-z0-9_-]*$

This shoudl do it for you

vks
  • 67,027
  • 10
  • 91
  • 124
4

The + means "one or more". Replace it with * for "zero or more".

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152