-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
  • 561,645
  • 34
  • 376
  • 476
Manoj
  • 1,533
  • 2
  • 11
  • 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
  • 713,503
  • 59
  • 514
  • 593
0

I think, if lowercase

[a-z]-?\d
Blaze349
  • 419
  • 1
  • 5
  • 15