Regex to disallow more than 1 dash consecutively

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

    ^[A-Za-z0-9]+(-[A-Za-z0-9]+)*$

    Using this regular expression, the hyphen is only matched just inside the group. This hyphen has the [A-Za-z0-9]+ sub-expression appearing on each side. Because this sub-expression matches on one or more alpha numeric characters, its not possible for a hyphen to match at the start, end or next to another hyphen.

提交回复
热议问题