Limit the number of words in a response with a regular expression
How about splitting the text with a regex (say "\s+") and then counting the length of the resulting list? That would be somewhat easier to read.
Could you try:
^(?:\b\w+\b[\s\r\n]*){1,250}$
That would limit to 250 words over multiple lines.
I am afraid that the Alan's initial proposition:
/^\w+(?:\s+\w+){0,249}$/
might be a case of catastrophic backtracking
When nesting repetition operators, make absolutely sure that there is only one way to match the same match
This has worked well for me, for example if you want a 6-word limit:
^(?:\w+\W+){0,5}(?:\w+)$