I can\'t figure out javascript regex that would satisfy all those requirements:
The string can only contain underscores and alphanumeric characters. It must
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