P粉9167604292023-09-01 00:35:05
This may work:
/^\s*(?:\S+\s+){0,249}\S*\s*$/
is different from \w
in your original regular expression that only matches [a-zA-Z0-9_]
, the special symbol \S
matches Any non-space character. Because character sets \S
and \s
are completely different, this should avoid any problems related to catastrophic backtracking.
\s*
. These are not counted. (\S \s )
A word consisting of 1 or more non-space characters followed by 1 or more space characters. {0,249}
Repeat up to 249 times\S*
Optionally add an extra word at the end, which should not end with a space. This is the 250th word, so the number of lines above needs to be 249 not 250. This only goes back the length of the last word, so may be slow if the last word is very long. However, growth cannot be exponential, so it should not cause Joi to collapse.