Regex to match '-' delimited alphanumeric words

前端 未结 4 1177
独厮守ぢ
独厮守ぢ 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:07

    Here you go (this works).

    var regExp = /^[A-Za-z0-9]+([-]{1}[A-Za-z0-9]+)+$/;
    

    letters and numbers greedy, single dash, repeat this combination, end with letters and numbers.

提交回复
热议问题