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 (e.g. “Cars, Vehicle, Large Dog, Bed” but not “Cars, Vehicle, Tiger)
An example of what I mean is this, these would be valid tags:
boy, man,girl, woman,tyrannosaurus rex, lion
And these would be invalid tags:
hat, cat, rat, c3po, @gmail
Because there are invalid characters in “@gmail”.
It should also be able to accept just a single tag, as long as the characters are alphanumeric.
Assuming you want to allow
_and not allow whitespace at the beginning or the end this would be the shortest solution:Introducing whitespace at the ends:
Removing
_from the allowed characters:This is the brute-force regex I initially posted. It translates your requirements to regex syntax. I would like to leave it here for reference.