1

I'm using this filter to minify my HTML. Unfortunately, that filter also minifies code inside <pre> tags, but I don't want them to be changed. How I can change the regexes so they don't minify any code inside <pre> tags?

s = Regex.Replace(s, @"\s+", " ");
s = Regex.Replace(s, @"\s*\n\s*", "\n");
s = Regex.Replace(s, @"\s*\>\s*\<\s*", "><");
s = Regex.Replace(s, @"<!--(.*?)-->", "");   //Remove comments
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
Jean Toledo
  • 71
  • 1
  • 7

1 Answers1

3

Until the developer of that filter provides this option, you can try the following: You can add a nested lookahead assertion to your regexes that precludes them from matching if a </pre> tag follows (unless a <pre> tag comes first). For the first three regexes, that means:

s = Regex.Replace(s, @"(?s)\s+(?!(?:(?!</?pre\b).)*</pre>)", " ");
s = Regex.Replace(s, @"(?s)\s*\n\s*(?!(?:(?!</?pre\b).)*</pre>)", "\n");
s = Regex.Replace(s, @"(?s)\s*\>\s*\<\s*(?!(?:(?!</?pre\b).)*</pre>)", "><");

Explanation of the lookahead assertion:

(?!          # Assert that the following regex can't be matched here:
 (?:         # Match...
  (?!        #  (unless the following can be matched:
   </?pre\b  #  an opening or closing <pre> tag)
  )          #  (End of inner lookahead assertion)
  .          # ...any character (the (?s) makes sure that this includes newlines)
 )*          # Repeat any number of times
 </pre>      # Match a closing <pre> tag
)            # (End of outer lookahead assertion)

For the fourth regex, we must first make sure that .*? doesn't match any <pre> tags either

s = Regex.Replace(s, @"(?s)<!--((?:(?!</?pre\b).)*?)-->(?!(?:(?!</?pre\b).)*</pre>)", "");

Other than that, the regex works just as above.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561