Split string by multiple delimiters
You can use a combination of the split
method and the Regexp.union
method like so:
delimiters = [',', ' ', "'"]word.split(Regexp.union(delimiters))# => ["Now", "is", "the", "time", "for", "all", "good", "people"]
You can even use regex patters in the delimiters.
delimiters = [',', /\s/, "'"]word.split(Regexp.union(delimiters))# => ["Now", "is", "the", "time", "for", "all", "good", "people"]
This solution has the advantage of allowing totally dynamic delimiters or any length.