input.split(/\s*[\s,]\s*/)
... \s* matches zero or more space characters (not just spaces, but also tabs and newlines).
... [\s,] matches one space character or one comma
If you want to avoid empty input elements like "foo,bar,,foobar" , this will do the trick:
input.split(/(\s*,?\s*)+/)
+ matches one or more of the preceding character or group.
Edit:
Added ? after a comma that matches zero or one comma.
Edit 2:
Turns off edit 1 was an error. I fixed it. Now for the expression you need to have at least one comma or one space.
KaptajnKold Apr 27 '12 at 7:50 2012-04-27 07:50
source share