I need to validate a string that can have lower-case characters and dashes but not at the beginning nor the end nor repeated. Then can have numbers too, but not at the beginning.
This is how it should work:
Match: ab9cd-a9bc-abbbc95
Not match: 5abcd
Not match: abbcd-
Not match: -abcd
Not match: abcd--abcd
I have been able to do everything but the last case, where a repeated dash shouldn't match.
I have this:
/^[a-z]+[a-z0-9\-]+[a-z0-9]$/
And I tried this but didn't work as expected:
/^[a-z]+[a-z0-9\-?]+[a-z0-9]$/
Another way to do it:
^[a-z](?:[a-z\d]|[a-z\d]\-)*[a-z\d]+$
Explained demo here: http://regex101.com/r/mE8pB8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With