I want to replace spaces and forward slashes with an underscore, and retain backslashes.
How do I do this?
I have the following regex:
"hello\h /123".gsub(/[\s+\/]/, "_")
#=> "helloh__123"
But it also replaces backslashes in the string.
I want to replace spaces and forward slashes with an underscore, and retain backslashes.
How do I do this?
I have the following regex:
"hello\h /123".gsub(/[\s+\/]/, "_")
#=> "helloh__123"
But it also replaces backslashes in the string.
So, if you just want to convert space and slash with underscore, you might prefer:
"hello\h /123".tr('/ ','__') # 2nd arg is two underscores
#=> "helloh__123"
(But I agree with the comments on the question so far.)