Kevin Kevin - 6 months ago 14
Ruby Question

Regex to match comma followed by whitespace?

I am trying to make a ruby regex to match tags in the format

Toys, Cars, Some Other Topic
but I can't figure out how to make it so that it splits it at after a comma and white space after but not if there is whitespace in a tag

This is what I have come up with http://rubular.com/r/ptjeQ1KyoD but is wrong for now.

/[\/,$\(\s+)]/

Answer

You can just use /,\s*/ (which is much simpler than what you've got!):

'Toys, Cars, Some Other Topic'.split /,\s*/
=> ["Toys", "Cars", "Some Other Topic"]