Adding Regular Expression minimum & maximum string limit to "\n" in word boundaries validation

390 Views Asked by At

If inputted text doesn't match with my pattern:

^(\b[\n]\w*\s*)+$

Which is if finding \n character in the inputted text then the text will not be validated, but I want to add min and max length to all whole string. I expect if the text doesn't match the pattern and string length lower than 3 and exceed 10 then it will not be validated, I know this following pattern is not correct but at least I'm trying to modify it like this:

^(\b[\n]\w{3,10}\s*)+$

For sample:

FrogFrog
FrogFrog <- it won't validated because has \n and exceed 10

Frog
Frog <- it won't validated because has \n

FrogFrogFrog <- it wont't validated because exceed than 10

FrogFrogFr <- it is valid because no \n character and not exceed than 10

Any correction or suggestions?

2

There are 2 best solutions below

2
aziz k'h On

try this regex :

^([\w\s]{3,10})(?!\n)$
0
The fourth bird On

You might start the match with a word character to match at least a character at the start.

Then repeat 2-9 times matching either a word character, space or tab using a character class and end the pattern with \z to assert the end of the string.

^\w[\w \t]{2,9}\z

For example a regex demo with match and a regex demo with no match.