Regex to match '-' delimited alphanumeric words

前端 未结 4 1168
独厮守ぢ
独厮守ぢ 2020-12-19 18:21

I would like to test if user type only alphanumeric value or one \"-\".

hello-world                 -> Match
hello-first-world           -> match
this-         


        
4条回答
  •  死守一世寂寞
    2020-12-19 19:02

    (^-)|-{2,}|[^a-zA-Z-]|(-$) looks for invalid characters, so zero matches to that pattern would satisfy your requirement.

提交回复
热议问题