There is a regular expression:
^[а-яё?\s]+$ How to supplement this regular season so that spaces are cut off at the beginning and at the end, and inside the word space is allowed? Ie "Ivan Ivanov" - skip, "Ivan Ivanov" - do not miss?
There is a regular expression:
^[а-яё?\s]+$ How to supplement this regular season so that spaces are cut off at the beginning and at the end, and inside the word space is allowed? Ie "Ivan Ivanov" - skip, "Ivan Ivanov" - do not miss?
I propose to parse the character class into spaces and everything else and add the following expression:
/^[а-яё?]+(?:\s+[а-яё?]+)*$/i or (if only 1 space between words is allowed):
/^[а-яё?]+(?:\s[а-яё?]+)*$/i See the demo .
It will find:
^ - beginning of line[а-яё?]+ - 1 or more Russian letters(?:\s[а-яё?]+)* - 0 or more:\s+ - 1 or more spaces (remove + if only 1 space is allowed between words)[а-яё?]+ - 1 or more Russian letters$ - end of lineUse .trim (); The original line does not change. Supported everywhere.
var str = " temp "; str.trim(); Is there some more:
jQuery.trim(); Here it is possible:
^(?:[^ ][а-яё?\s]+[^ ]?|[^ ]?[а-яё?\s]+[^ ])$ If you answer the question, then
^[^\s][а-яё?\s]*[^\s]$ But it may be better to call trim() ?
[а-яё?\s]+? - this is a mistake, in my opinion - Yuri+? - this is a lazy quantifier + ), but since you also require at least 2 characters in the string. - Wiktor Stribiżew[^\s] requires a symbol. But this, I think, is not so relevant. By the way, the lazy quantifier is out of place here, it only slows down the engine. - Wiktor Stribiżew<> . Test regex101.com/r/b7OZfd/1 - VismanSource: https://ru.stackoverflow.com/questions/610811/
All Articles
trim()? Give an example of code that does not work for you. - Wiktor Stribiżew/^[а-яё?]+(?:\s[а-яё?]+)*$/i- Wiktor Stribiżew