-1

I am trying to use this to remove all comments from a string.

str = str.replace(/<!--(.*?)-->/g, ""); 

It works perfectly in chrome, but when it comes to run in IE8, surprise surprise, it does not remove any part of the text.

I noticed that when chrome would get rid of a comment, they would turn into something aliong these lines:

&lt;!--[if !supportLists]--&gt;· &lt;!--[endif]--&gt;

Here is my full function, based on This Link

function CleanWordHTML(str) {
    str = str.replace(/<o:p>\s*<\/o:p>/g, "");
    str = str.replace(/<o:p>.*?<\/o:p>/g, "&nbsp;");
    str = str.replace(/\s*mso-[^:]+:[^;"]+;?/gi, "");
    str = str.replace(/\s*MARGIN: 0cm 0cm 0pt\s*;/gi, "");
    str = str.replace(/\s*MARGIN: 0cm 0cm 0pt\s*"/gi, "\"");
    str = str.replace(/\s*TEXT-INDENT: 0cm\s*;/gi, "");
    str = str.replace(/\s*TEXT-INDENT: 0cm\s*"/gi, "\"");
    str = str.replace(/\s*TEXT-ALIGN: [^\s;]+;?"/gi, "\"");
    str = str.replace(/\s*PAGE-BREAK-BEFORE: [^\s;]+;?"/gi, "\"");
    str = str.replace(/\s*FONT-VARIANT: [^\s;]+;?"/gi, "\"");
    str = str.replace(/\s*tab-stops:[^;"]*;?/gi, "");
    str = str.replace(/\s*tab-stops:[^"]*/gi, "");
    str = str.replace(/\s*face="[^"]*"/gi, "");
    str = str.replace(/\s*face=[^ >]*/gi, "");
    str = str.replace(/\s*FONT-FAMILY:[^;"]*;?/gi, "");
    str = str.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3");
    str = str.replace(/<(\w[^>]*) style="([^\"]*)"([^>]*)/gi, "<$1$3");
    str = str.replace(/\s*style="\s*"/gi, '');
    str = str.replace(/<SPAN\s*[^>]*>\s*&nbsp;\s*<\/SPAN>/gi, '&nbsp;');
    str = str.replace(/<SPAN\s*[^>]*><\/SPAN>/gi, '');
    str = str.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3");
    str = str.replace(/<SPAN\s*>(.*?)<\/SPAN>/gi, '$1');
    str = str.replace(/<FONT\s*>(.*?)<\/FONT>/gi, '$1');
    str = str.replace(/<\\?\?xml[^>]*>/gi, "");
    str = str.replace(/<\/?\w+:[^>]*>/gi, "");
    str = str.replace(/<H\d>\s*<\/H\d>/gi, '');
    str = str.replace(/<H1([^>]*)>/gi, '');
    str = str.replace(/<H2([^>]*)>/gi, '');
    str = str.replace(/<H3([^>]*)>/gi, '');
    str = str.replace(/<H4([^>]*)>/gi, '');
    str = str.replace(/<H5([^>]*)>/gi, '');
    str = str.replace(/<H6([^>]*)>/gi, '');
    str = str.replace(/<\/H\d>/gi, '<br>'); //remove this to take out breaks where Heading tags were 
    str = str.replace(/<(U|I|STRIKE)>&nbsp;<\/\1>/g, '&nbsp;');
    str = str.replace(/<(B|b)>&nbsp;<\/\b|B>/g, '');
    str = str.replace(/<([^\s>]+)[^>]*>\s*<\/\1>/g, '');
    str = str.replace(/<([^\s>]+)[^>]*>\s*<\/\1>/g, '');
    str = str.replace(/<([^\s>]+)[^>]*>\s*<\/\1>/g, '');
    //comments
    str = str.replace(/<!--(.*?)-->/g, "");
    //spaces
    str = str.replace(/&nbsp;/g, '');
    //empty tags
    str = str.replace(/<[^\/>][^>]*><\/[^>]+>/g, '');


    //some RegEx code for the picky browsers
    var re = new RegExp("(<P)([^>]*>.*?)(<\/P>)", "gi");
    str = str.replace(re, "<div$2</div>");
    var re2 = new RegExp("(<font|<FONT)([^*>]*>.*?)(<\/FONT>|<\/font>)", "gi");
    str = str.replace(re2, "<div$2</div>");
    str = str.replace(/size|SIZE = ([\d]{1})/g, '');

    return str;
}

Thank you

starvator
  • 989
  • 1
  • 11
  • 26
  • obligatory remark: http://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not – epascarello Dec 02 '14 at 15:24
  • @epascarello regardless, shouldn't there still be a way to grab a – starvator Dec 02 '14 at 15:27
  • 1
    Have you tried `<!--(.*?)-->`? Maybe I'm way off, but I'd suspect that IE8 might be removing the HTML comments before parsing the script text. (i.e., it's reading it as `` and then running the intermediate `...` sections). Then again, that would seem to turn your regex into a `//` JS comment, so I'm probably wrong. – apsillers Dec 02 '14 at 15:35
  • @apsillers I swear I already tried `str = str.replace(/<!--(.*?)-->/g, "");`. I guess I made a typo in it because this time around, it works! Thank you! – starvator Dec 02 '14 at 15:44

1 Answers1

0

As apsillers mentioned in the comments, I can just use this:

str = str.replace(/&lt;!--(.*?)--&gt;/g, "");
starvator
  • 989
  • 1
  • 11
  • 26