How can I improve this regular expression?

Posted by Michael Haren on Stack Overflow See other posts from Stack Overflow or by Michael Haren
Published on 2010-03-24T22:22:50Z Indexed on 2010/03/24 22:33 UTC
Read the original article Hit count: 153

Filed under:
|

I want a regular expression to match valid input into a Tags input field with the following properties:

  • 1-5 tags
  • Each tag is 1-30 characters long
  • Valid tag characters are [a-zA-Z0-9-]
  • input and tags can be separated by any amount of whitespace

Here's what I have so far--it seems to work but I'm interested how it could be simplified or if it has any major flaws:

\s*[a-zA-Z0-9-]{1,30}(\s+[a-zA-Z0-9-]{1,30}){0,4}\s*

// that is: 
\s*                          // match all beginning whitespace
[a-zA-Z0-9-]{1,30}           // match the first tag
(\s+[a-zA-Z0-9-]{1,30}){0,4} // match all subsequent tags
\s*                          // match all ending whitespace

Preprocessing the input to make the whitespace issue easier isn't an option (e.g. trimming or adding a space).

If it matters, this will be used in javascript. Any suggestions would be appreciated, thanks!

© Stack Overflow or respective owner

Related posts about regex

Related posts about pattern-matching