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.
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)
Regexp::MULTILINE
== 4
–
Pusillanimity Regexp::MULTILINE
? –
Alexipharmic © 2022 - 2024 — McMap. All rights reserved.
re = Regexp.new reg_str
tore = Regexp.new reg_str, 4
so that it matches multiple lines. – Cyd