0

In my Ruby codes I'm removing all new lines, tabs and HTML comments.

But when I do this all the codes inside <pre> turning one line too.

module Jekyll
    module HtmlCompressor
        def htmlCompressor(data)
            data.gsub("\n", "").gsub("\t", "").gsub(/<!--(?!<!)[^\[>].*?-->/, "")
        end
    end
end

Liquid::Template.register_filter(Jekyll::HtmlCompressor)

How can I modify this regex to only work outside <pre> blocks?

ozgrozer
  • 1,824
  • 1
  • 23
  • 35
  • 4
    Use an HTML parser (like nokogiri) and parse the HTML with it instead of using regular expressions (which can never fully work). HTML just can not be parsed with regular expression. – Holger Just Jun 06 '16 at 15:54
  • 2
    This [StackOverflow answer](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) clearly demonstrates you shouldn't use regex-es to parse HTML. – Rudy Velthuis Jun 07 '16 at 15:07

0 Answers0