Skip to content
Advertisement

Regex for not containing consecutive characters

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

The string can only contain underscores and alphanumeric characters. It must begin with a letter, not include spaces, not end with an underscore, and not contain two consecutive underscores.

This is as far as I came, but ‘not containing consecutive underscores’ part is the hardest to add.

JavaScript

Advertisement

Answer

You could use multiple lookaheads (neg. ones in this case):

JavaScript

See a demo on regex101.com.


Broken down this says:
JavaScript


As JavaScript snippet:

JavaScript

Please do not restrain passwords!

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement