How to replace the last occurrence of a substring in ruby?

前端 未结 10 1186
南笙
南笙 2021-02-03 20:08

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

10条回答
  •  广开言路
    2021-02-03 20:58

    .gsub /abc(?=[^abc]*$)/, 'ABC'
    

    Matches a "abc" and then asserts ((?=) is positive lookahead) that no other characters up to the end of the string are "abc".

提交回复
热议问题