Regex to disallow more than 1 dash consecutively

前端 未结 5 965
误落风尘
误落风尘 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:24

    ^(?!-)(?!.*--)[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
    (?

提交回复
热议问题