Regex to disallow more than 1 dash consecutively

^(?!-)(?!.*--)[A-Za-z0-9-]+(?<!-)$

Explanation:

^             # Anchor at start of string
(?!-)         # Assert that the first character isn't a -
(?!.*--)      # Assert that there are no -- present anywhere
[A-Za-z0-9-]+ # Match one or more allowed characters
(?<!-)        # Assert that the last one isn't a -
$             # Anchor at end of string

Leave a Comment