Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何替换字符串 'nEEdle' 以获得以下结果:
"haystackhaystacknEEdlehaystack" -> "haystackhaystack<b>nEEdle</b>haystack"
在我的应用程序中,我只有小写的搜索参数,所以我想获取最后一个正则表达式结果 ($~) 并将其用作替换字符串。以下方法不起作用:
n = "needle" haystack.gsub(/#{n}/i, "<b>#{$~}</b>")
有什么提示吗?
尝试:
heystack.gsub(/#{n}/i, '<b>\0</b>')