Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

regex - Limit the number of words in a response with a regular expression

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 w+ but I had trouble getting it work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Could you try:

^(?:w+[s
]*){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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...