To split the string you can use String#split
'hey#man^you*are#awesome'.split(/[@#$%^&*]/)
#=> ["hey", "man", "you", "are", "awesome"]
to bring it back together, you can use Array#join
'hey#man^you*are#awesome'.split(/[@#$%^&*]/).join(' ')
#=> "hey man you are awesome"
split
and join
should be self-explanatory. The interesting part is the regular expression /[@#$%^&*]/
which matches any of the characters inside the character class [...]
. The above code is essentially equivalent to
'hey#man^you*are#awesome'.gsub(/[@#$%^&*]/, ' ')
#=> "hey man you are awesome"
where the gsub
means "globally substitute any occurence of @#$%^&* with a space".