admin管理员组

文章数量:1287933

I need a regular expression to simply limit the number of characters in a textarea to 5000. If there are more secure regular expressions I could use, I am open to suggestions.

Thanks

I need a regular expression to simply limit the number of characters in a textarea to 5000. If there are more secure regular expressions I could use, I am open to suggestions.

Thanks

Share Improve this question edited Apr 1, 2011 at 16:44 Stephan 43.1k66 gold badges244 silver badges340 bronze badges asked Mar 31, 2011 at 15:53 user671460user671460 211 silver badge2 bronze badges 3
  • 1 What framework are you using? there may be a built in mechanism for this. – corsiKa Commented Mar 31, 2011 at 15:56
  • 2 Just seems like a totally odd thing to do. Why use a Regex for this at all? Surely a simple limit on the string length of the textarea text would be easier to manage and avoid starting the regular expression engine every time someone adds something to the text? – glenatron Commented Mar 31, 2011 at 15:56
  • I am using cForms for WordPress. It has built-in support for RegEx, but I'm not sure how to go about adding a string limit length to it. – user671460 Commented Mar 31, 2011 at 16:46
Add a ment  | 

2 Answers 2

Reset to default 8

^.{0,5000}$ should work. Although using a regular expression for this is probably an odd choice.

You will always get the job done with:

^[\s\S]{0,5000}$

  • First there´s a character class that says: match whitespace (\s) or non-whitespace (\S) -- i.e any possible character
  • Repeat that between zero and 5000 times.

本文标签: javascriptRegular Expression to limit characters to 5000Stack Overflow