I have tried looking over quite a lot of regex guides on how to use the negative lookbehind and lookahead in my regex function for html5 pattern.
I am trying to match the following pattern, where the string has to start and end with a [a-z]
letter. The string can be up to 30 characters long. Can also include the symbol: -
, however it can never have more than one -
in a row.
Sooo, what I came up with so far is this:
^[a-z][a-z(?<!-(-)?!-)]{0,28}[a-z]$
Now I could not get the lookahead and lookbehind so work properly and I am not quite sure if I implemented the max 30 characters correctly. However, I have tried starting and ending with [a-z]
and it works fine.
Some example strings:
'a-b' => true
'a-' => false
'-a' => false
'a--b' => false
'ab-cd' => true
'abc' => true
'a-b-c' => true