I'm trying to build a regular expression with the following conditions:
And this is what I have done so far:
^(([A-Za-z]+( [A-Za-z])+){1,50})$
This allows me to validate the spaces between words and only letters conditions but it is not working for the length and it's not working for words without spaces, example:hello. Can someone help me with this?
Example:
What I need: Regex that allows sentences (with max length 50) like this:
Hello this is an example
Hello
a b c
Try this:
^\b(?!.*?\s{2})[A-Za-z ]{1,50}\b$
Demo
[A-Za-z ]{1,50} will check for the characters and length, while the negative lookahead (?!.*?\s{2}) will check for the spaces condition. (\b) to disallow white space at ends.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With