deanhume / html-minifier

A simple command line tool to minify your HTML, Razor views & Web Forms views
http://deanhume.github.io/html-minifier/
MIT License
134 stars 86 forks source link

Regex issue with javascript line comments #47

Open qoreycube opened 6 years ago

qoreycube commented 6 years ago

Line 161 in StreamReaderExtension.cs: htmlContents = Regex.Replace(htmlContents, @"// (.*?)\r?\n", "", RegexOptions.Singleline);

This catches regex expressions in javascripts that have escaped forward slashes. For example: url += $('#field').val().replace(/\//g, '').replace(/&/g, 'and').substring(0, 50) + '/';

the regex expression "/\//g" is valid, but not a comment.

Trying to think of a workaround for this line, but my regex is a little rusty.

I was thinking this:
htmlContents = Regex.Replace(htmlContents, @"[^\]// (.*?)\r?\n", "", RegexOptions.Singleline);

But that also fails.

qoreycube commented 6 years ago

Actually, I correct myself. I think it might be here.

javaScriptComments = javaScriptComments.Replace(scriptBlock, Regex.Replace(scriptBlock, @"[^:|""|']//(.*?)\r?\n", ""));