I want to replace the last occurrence of a substring in Ruby. What\'s the easiest way? For example, in abc123abc123, I want to replace the last abc
You can achieve this with String#sub and greedy regexp .* like this:
.*
'abc123abc123'.sub(/(.*)abc/, '\1ABC')