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,080
  • 8
  • 34
  • 48

2 Answers2

4

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

chrylis -cautiouslyoptimistic-
  • 72,004
  • 20
  • 117
  • 147
3
^[A-Za-z][A-Za-z0-9_-]*$

This shoudl do it for you

vks
  • 65,133
  • 10
  • 87
  • 119