Is there a Jekyll filter that will replace text using a regular expression (regex) filter?
I believe the "built-in" filter replace
does simple string substitution.
In the event there isn't a (better) solution, I'll throw in the very obviously simple plugin that will do the trick - drop this into your _plugins/
folder as the file regex_filter.rb
- it takes the regex as a string, as the first arg, and the replacement as the second arg (for example, {{ page.url | replace_regex: '/$', '' }}
:
module Jekyll
module RegexFilter
def replace_regex(input, reg_str, repl_str)
re = Regexp.new reg_str
# This will be returned
input.gsub re, repl_str
end
end
end
Liquid::Template.register_filter(Jekyll::RegexFilter)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With