Regex to disallow more than 1 dash consecutively

前端 未结 5 964
误落风尘
误落风尘 2020-11-28 10:44
  1. How can I disallow -- (more than 1 consecutive -)? e.g. ab--c
  2. - at the back of words not allow, e.g. abc-<
5条回答
  •  春和景丽
    2020-11-28 11:33

    ^[a-zA-Z0-9](?!.*--)[a-zA-Z0-9-]*[a-zA-Z0-9]$
    
    ^[a-zA-Z0-9]     /*Starts with a letter or a number*/
    (?!.*--)         /*Doesn't include 2 dashes in a row*/
    [a-zA-Z0-9-]*    /*After first character, allow letters or numbers or dashes*/
    [a-zA-Z0-9]$     /*Ends with a letter or a number*/
    

    Matches:

    Re-play / Re-play-ed

    Doesn't Match:

    Replay- / Re--Play / -Replay

提交回复
热议问题