Regex for not containing consecutive characters

后端 未结 3 1781
鱼传尺愫
鱼传尺愫 2021-01-06 06:48

I can\'t figure out javascript regex that would satisfy all those requirements:

The string can only contain underscores and alphanumeric characters. It must

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-06 07:29

    You can also use

    ^[a-zA-Z]([a-zA-Z0-9]|(_(?!_)))+[a-zA-Z0-9]$
    

    Demo

    The only change comparing to your regex is changing [a-zA-Z0-9_] to [a-zA-Z0-9]|(_(?!_)). I removed underscore from the character set and allow it in the second part of the alternative if it's not followed by another one.

    (?!_) is negative lookahead meaning that _ cannot be next character

提交回复
热议问题