I would like to test if user type only alphanumeric value or one \"-\".
hello-world -> Match
hello-first-world -> match
this-
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.