From e575c1f131bc8a50d91fd698559729c184ab736f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20S=C3=B8gaard?= Date: Tue, 30 Mar 2021 12:34:01 +0200 Subject: [PATCH] Add replace_last and remove_last filters --- lib/liquid/standardfilters.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index cf92e687..c09c5a70 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -268,6 +268,11 @@ module Liquid input.to_s.sub(string.to_s, replacement.to_s) end + # Replace the last occurrences of a string with another + def replace_last(input, string, replacement = '') + input.to_s.reserve.sub(string.to_s.reserve, replacement.to_s.reserve).reserve + end + # remove a substring def remove(input, string) input.to_s.gsub(string.to_s, '') @@ -278,6 +283,11 @@ module Liquid input.to_s.sub(string.to_s, '') end + # remove the last occurences of a substring + def remove_last(input, string) + input.to_s.reverse.sub(string.to_s.reverse, '').reverse + end + # add one string to another def append(input, string) input.to_s + string.to_s