In order to encompass all Russian letters, relying solely on the [А-Яа-я]
range is insufficient. It is necessary to also include the letter [ёЁ]
in the range as it is not covered by that particular set.
Additionally, when there is an unescaped hyphen positioned between literal symbols within a character class, it forms a range, and it is advisable to place it at the beginning or end of the character class for clarity.
If you want to impose limitations like ensuring there are at least N occurrences of something, anchored lookaheads need to be incorporated.
var nameRegex = /^(?=[^A-ZА-ЯЁ]*[A-ZА-ЯЁ])(?=[^0-9]*[0-9])[-A-Z0-9А-ЯЁ.+~_!?*]+$/i;
Below is the demonstration link
In this context, ^
anchors the pattern at the beginning of the string, $
anchors it at the end, (?=[^A-ZА-ЯЁ]*[A-ZА-ЯЁ])
necessitates at least one letter, while (?=[^0-9]*[0-9])
demands at least one digit.
Note that I excluded all lowercase letters because the case-insensitive modifier /i
renders them unnecessary.
To exclusively match symbols from the specified list, employ a simple +
quantifier:
var nameRegex = /^[-A-Z0-9А-ЯЁ.+~_!?*]+$/i;
^
If allowing an empty string, opt for *
over +
.