Regex to match '-' delimited alphanumeric words

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

    Try this:

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

    This will only match sequences of one or more sequences of alphanumeric characters separated by a single -. If you do not want to allow single words (e.g. just hello), replace the * multiplier with + to allow only one or more repetitions of the last group.

提交回复
热议问题