Regex for [a-zA-Z0-9\-] with dashes allowed in between but not at the start or end

后端 未结 4 2114
逝去的感伤
逝去的感伤 2021-02-14 07:37

Update:

This question was an epic failure, but here\'s the working solution. It\'s based on Gumbo\'s answer (Gumbo\'s was close to working so I chose it as the accepte

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-14 08:14

    It should be something like this:

    ^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$
    

    You are telling it to look for only one char, either a-z, A-Z, 0-9 or -, that is what [] does.

    So if you do [abc] you will match only "a", or "b" or "c". not "abc"

    Have fun.

提交回复
热议问题