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
CodePudding user response:
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 [ _-]
.
CodePudding user response:
If the regex pattern ends with a letter or digit then it won't end with something else
^[A-Za-z0-9-][A-Za-z0-9_ -]{0,29}[A-Za-z0-9]$