I'm new to RegEx and JavaScript and I was wondering if anyone knew what the RegEx would be for detecting whether or not an input field contained the following type of format:
At least one alphanumeric tag which can contain spaces (e.g. "Test Tag" but not "Test@Tag")
Each tag separated by a single comma and nothing else, no spaces. (e.g. "Cars,Vehicle,Tiger Cactus, Bed")
An example of what I mean is this, these would be valid tags:
boy,man,girl,woman,tyran开发者_StackOverflow社区nosaurus rex,lion
And these would be invalid tags:
hat, cat, rat, c3po, @gmail
Because there are spaces between the tags and invalid characters.
Also, how would I count the tags for imposing a limit?
Edited for clarification!
(?:([\w\s]+)(?:,\s*))*
would get any number of tags separated by commas.
(?:([\w\s]+)(?:,\s*)){n}
would match n tags where n is an integer.
(?:([\w\s]+)(?:,\s*)){0,n}
would match 0 to n tags where n is an integer.
In order to count the tags, you'll need to check the number of captured groups.
精彩评论