1

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.

5
  • are you validating the input or trying to extract the tag names from the string? if you're just validating the input, it will be a much simpler regex. Commented Nov 30, 2010 at 17:48
  • Just validating the input for now, the extracting part will be done later. Commented Nov 30, 2010 at 17:51
  • Do you want to allow extra spaces at the beginning or at the end? Commented Nov 30, 2010 at 18:00
  • _'s should be invalid and extra spaces are allowed as that stuff will be filtered server-side. Commented Nov 30, 2010 at 18:02
  • 1
    Possible duplicate of For alphanumeric tags separated by commas? Commented Jan 27, 2016 at 22:43

4 Answers 4

2

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*$/ 
Sign up to request clarification or add additional context in comments.

3 Comments

One more thing, if your don't mind, how would I allow just a single space inside a tag and no more? So something like this is vaid "Car, tyrannosaurus rex" but "Car, Tyran nosaurus rex, Bike" is invalid?
@ThunderLegs None of the solutions you received take into account a situation like that so altering one of them is out of the question.
@ThunderLegs I added another answer with a separate regex for allowing at most two words.
2

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.

8 Comments

bah,, true, not ,, ok true. It's too permissive. Also I don't know if he wants to allow _.
No, both of those evaluate to false. Only 1 comma is allowed with ",?". You are correct that \w allows the underscore character. He could use [a-zA-Z] if he wants to disallow it. Overall he might want to make it more permissive by allowing multiple spaces after the comma or spaces before the comma, but the regexp works as requested.
Thanks, this works well and is the cleanest of all the regex posted. Out of curiosity, how would I allow multiple spaces as you have described?
@James /^(\w+,? *)+$/ would allow spaces only after the ,, and only spaces, not any white-space.
@Alin - Yes, that is true. That's what I believe he was asking. Add another " *" before the ",?" if you want to allow spaces before the comma. If you don't want to allow a trailing comma, then something like this would work: /^(\w+,? *)*\w+ *$/
|
1

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 

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.