I have the following regex:
/([A-Za-z0-9]+)([A-Za-z0-9\-\_]+)([A-Za-z0-9]+)/
It is not working according to my needs, which are:
- do not allow spaces
- allow capital English letters
- allow lowercased English letters
- allow digits
- the string may not contain both a hyphen and an underscore
- hyphen: hyphen cannot be at the beginning or at the end of the string;
There can be any amount of hyphens but consecutively there can be only 1 hyphen (
a--b
is invalid). - underscores: underscore cannot be at the beginning or at the end of the string; There can be any amount of underscores but consecutively there can be only 1 underscore (
a__b
is invalid) - the string must contain at least 1 character (letter)
Valid examples:
a1_b_2_hello
2b-ffg-er2
abs
123a
Invalid examples:
_a1_b_2_hello
2b-ffg_er2-
__
--
a__
b--2