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.

Jonathan
  • 592
  • 2
  • 7
  • 18
  • 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! – Andy Mikula Feb 17 '09 at 22:25
  • What about punctuation? Are requiring your users to type in only word characters and whitespace? – Alan Moore Feb 18 '09 at 11:32

6 Answers6

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

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 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. – Jonathan Feb 17 '09 at 19:25
  • This works for me except if I enter " , @ it returns false. Can you tell me what I need to do ? Thanks – iOSAppDev Dec 11 '13 at 08:22
  • @iOSAppDev word boundaries (http://www.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"). – VonC Dec 11 '13 at 08:33
  • 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. – iOSAppDev Dec 11 '13 at 08:47
  • @iOSAppDev 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). – VonC Dec 11 '13 at 08:49
  • Thanks VonC. I will ask a new question with link to this question – iOSAppDev Dec 11 '13 at 08:53
  • I have added separate question here http://stackoverflow.com/questions/20515131/restrict-the-user-to-enter-max-50-words-in-uitextview-ios – iOSAppDev Dec 11 '13 at 09:21
  • how to ignore space at the end? "a "is invalid while "a a" is valid – Junaid Masood Aug 19 '20 at 11:54
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.

Jay Kominek
  • 8,674
  • 1
  • 34
  • 51
2

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

^(?:\w+\W+){0,5}(?:\w+)$
jsiegal
  • 23
  • 3
1

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

(\w*\W*){0,250}
Mohamad Shawkey
  • 135
  • 1
  • 11
  • this worked for me even when i add comma(,) for number of words defined we are looking at in specified text field. – Pallavi May 20 '21 at 14:37
  • How to add for the above statement that it should accept space only and null along with above expression – Pallavi May 20 '21 at 14:38
1

This worked for me:

^\W*(\w+(\W+|$)){1,250}$
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Junaid Masood
  • 658
  • 11
  • 20
  • 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}$ – David Hammond Apr 30 '21 at 20:14
1

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}$
janksmap
  • 61
  • 1
  • 2
  • 7