I tried writing this: ^[A-Za-z0-9-][A-Za-z0-9- ]{1,30}$
But the regex should not start and end with a special character or space.
Sample values It should allow:
EcoLight
Eco-Light
Eco_Light
Eco Light
Eco Light 1
>Solution :
One option would be to use:
^(?!.{31})[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$
See an online demo. The negative lookahead will prevent over 30 characters while the non-capture group allows for multiple groups of characters that are delimited through any of the characters in the class [ _-].