Limit the number of words in a response with a regular expression
Asked Answered
P

6

14

Does anybody have a regular expression that would work to limit the number of words in a response? For instance, I'd like to use it with jQuery validate so I can restrict a textbox/textarea to have say 250 words. The boxes will be plain-text.

I've done some Googling but none of the ones I've found were very good. They mostly centered around doing \b\w+\b but I had trouble getting it work.

Pudency answered 17/2, 2009 at 16:52 Comment(2)
It might make more sense to count characters - there's nothing stopping people from just typing and typing and typing with no spaces...that said, VonC's answer looks excellent!Wiedmann
What about punctuation? Are requiring your users to type in only word characters and whitespace?Haematosis
H
14

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


To allow for basic punctuation, see janksmap's answer.

^(?:\b\w+\b[\s\r\n\.\,\?\!]*){0,250}$

For non-Latin script (like Hebrew), see this answer.

Huron answered 17/2, 2009 at 18:44 Comment(8)
This seemed to work in my tests. I'm not sure if this version has any downsides but I'm going to accept this one for now.Pudency
This works for me except if I enter " , @ it returns false. Can you tell me what I need to do ? ThanksTingaling
@Tingaling word boundaries (regular-expressions.info/wordboundaries.html) depend on the regex flavor you are using. If simply adding the quotes as a separator in my original regex (ie: ^(?:\b\w+\b["\s\r\n]*){1,250}$) doesn't work, then it is best to look at alternative solution (meaning "beside regex").Huron
Thanks for link. Can you suggest any alternative solution ? I want to restrict user to enter max 150 words in textview. User can enter anything or copy & paste in text box.Tingaling
@Tingaling not on the top of my head, but that would be a good question to ask (with a link back to this one, and with lots of details about the error you got and why it doesn't work).Huron
Thanks VonC. I will ask a new question with link to this questionTingaling
I have added separate question here #20515631Tingaling
how to ignore space at the end? "a "is invalid while "a a" is validBulganin
H
3

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.

Hatpin answered 17/2, 2009 at 16:56 Comment(0)
I
2

This has worked well for me, for example if you want a 6-word limit:

^(?:\w+\W+){0,5}(?:\w+)$
Invocate answered 15/9, 2016 at 21:20 Comment(0)
O
2

To expand VonC's response, I found that by slightly modifying it, you can allow for basic punctuation and anywhere from 0 to 250 words.

^(?:\b\w+\b[\s\r\n\.\,\?\!]*){0,250}$
Omniscience answered 29/9, 2022 at 20:12 Comment(0)
T
1

Here is the regular expression I use for the word count validation.

(\w*\W*){0,250}
Tephra answered 21/2, 2016 at 14:26 Comment(2)
this worked for me even when i add comma(,) for number of words defined we are looking at in specified text field.Addend
How to add for the above statement that it should accept space only and null along with above expressionAddend
B
1

This worked for me:

^\W*(\w+(\W+|$)){1,250}$
Bulganin answered 19/8, 2020 at 11:57 Comment(1)
This worked better than the accepted answer for me. I modified it to treat periods and commas as word characters, which is helpful when someone enters numbers or domain names, and mirrors the way Word counts words. ^\W*(?:[\w\.\,]+(?:\W+|$)){1,250}$Wilburn

© 2022 - 2024 — McMap. All rights reserved.