我想用下划线替换空格和正斜杠,并保留反斜杠。
我该怎么做?
我有以下正则表达式:
"hello\h /123".gsub(/[\s+\/]/, "_")
#=> "helloh__123"
但它也替换了字符串中的反斜杠。
答案 0 :(得分:0)
因此,如果您只想转换空格并使用下划线斜杠,您可能更喜欢:
"hello\h /123".tr('/ ','__') # 2nd arg is two underscores
#=> "helloh__123"
(但我同意迄今为止对该问题的评论。)