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 开发者_StackOverflow中文版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:
/^\w(\s*,?\s*\w)*$/
Introducing whitespace at the ends:
/^\s*\w(\s*,?\s*\w)*\s*$/
Removing _
from the allowed characters:
/^\s*[a-z0-9](\s*,?\s*[a-z0-9])*\s*$/
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.
/^\s*([a-z0-9]+(\s[a-z0-9]+)*)(\s*,\s*([a-z0-9]+(\s[a-z0-9]+)*))*\s*$/
Try something like this:
var re = /^(\w+,? ?)+$/;
var str1 = "boy, man,girl, woman,tyrannosaurus rex, lion";
var str2 = "hat, cat, rat, c3po, @gmail";
alert(str1.test(re)); // true
alert(str2.test(re)); // false
Breaking it down... \w matches word characters, \w+ matches 1 or more word characters. ,? ? matches optional comma and space. (Two commas would be rejected.) The ()+ around everything says one or more times. Lastly ^ and $ anchors it to the beginning and end of the string to make sure everything is matched.
Assuming that underscores (_
) are not invalid:
/^(\w+\s?[\w\s]*)(,\s*\w+\s?[\w\s]*)*$/
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match the regular expression below and capture its match into backreference number 1 «(\w+\s?[\w\s]*)»
Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match a single character present in the list below «[\w\s]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
A word character (letters, digits, and underscores) «\w»
A whitespace character (spaces, tabs, and line breaks) «\s»
Match the regular expression below and capture its match into backreference number 2 «(,\s*\w+\s?[\w\s]*)*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Note: You repeated the capturing group itself.
The group will capture only the last iteration.
Put a capturing group around the repeated group to capture all iterations. «*»
Match the character “,” literally «,»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match a single character present in the list below «[\w\s]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
A word character (letters, digits, and underscores) «\w»
A whitespace character (spaces, tabs, and line breaks) «\s»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
Created with RegexBuddy
A separate question is being answered here. How to do the same thing but allow tags with at most two words?
/^\s*[a-z0-9]+(\s+[a-z0-9]+)?(\s*,\s*[a-z0-9]+(\s+[a-z0-9]+)?)*\s*$/
Tested.
精彩评论