我的情况:
text = "[A] and [B]"
search = ["[A]", "[B]", "[C]", "[D]", "[Aa]"]
replace = ["[B]", "[C]", "[D]", "[E]", "[Bb]"]
我想将 text
的值替换为 "[B] and [C]"
我该怎么做?非常感谢!
最佳答案
text.gsub(/\[\w+\]/, Hash[search.zip replace])
# => "[B] and [C]"
关于Ruby:如何用两个数组替换字符串中的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29249887/