Regex to disallow more than 1 dash consecutively

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

    If “-” is not allowed at the beginning nor end of the string, you are searching for a sequence of “one or more alanum, followed by one or more group(s) of one dash followed by 1 or more alanum”

    /[0-9A-Z]+(-[0-9A-Z]+)+/
    

    Simple is a valuable motto with regular expressions. (nota : to search small case characters, add them. I didn't for clarity)

提交回复
热议问题