-1

I wanna regex which will allow the following samples

1) abcd
2) abcd123
3) abcd-123

which should not allow

1) 123abcd
2) 123
3) 123-123
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Manoj
  • 1,833
  • 3
  • 14
  • 11

2 Answers2

2

You can use this regex:

/^[a-z]+-?\d*$/i

RegEx Breakup:

  • ^ - Assert start
  • [a-z]+ - Match 1 or more alphabets
  • -? - Match an optional hyphen
  • \d* - Match 0 or more digits
  • $ - Assert end
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

I think, if lowercase

[a-z]-?\d
Blaze349
  • 429
  • 1
  • 5
  • 16