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!