Regular expression that accepts alphanumeric , non consecutive dash and non consecutive white space

后端 未结 4 1160
孤城傲影
孤城傲影 2020-12-10 23:33

Can anyone help me create a regular expression that accepts alphanumeric (numbers and letters only) and dashes and white spaces.

It shouldn\'t accept consecutive das

4条回答
  •  清歌不尽
    2020-12-11 00:17

    Try this:

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

    When the first [A-Za-z0-9]+ runs out of letters and digits, the [\s-] inside the group tries to match a hyphen or a whitespace character. If it succeeds, the second [A-Za-z0-9]+ tries to match some more alphanumerics. And the group gets repeated as many times as necessary.

提交回复
热议问题