使用正则表达式限制响应中的单词数 [英] Limit the number of words in a response with a regular expression
问题描述
有没有人有一个正则表达式可以限制响应中的单词数?例如,我想将它与 jQuery 验证一起使用,这样我就可以将文本框/文本区域限制为 250 个单词.这些框将是纯文本的.
我已经进行了一些谷歌搜索,但我发现的都不是很好.他们主要集中在做 \b\w+\b 但我很难让它工作.
你可以试试:
^(?:\b\w+\b[\s\r\n]*){1,250}$
这将限制为多行 250 个单词.
恐怕是艾伦最初的提议:
/^\w+(?:\s+\w+){0,249}$/
可能是灾难性回溯><块引用>
嵌套重复操作符时,请确保只有一种方法可以匹配相同的匹配项
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.
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
这篇关于使用正则表达式限制响应中的单词数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!