I played around with this one a bit more and, if you want to strip orphan closing comment tags (-->'s with no leading <!--'s) as well, use this prototype function instead of the one on my demo page above:
Code:
String.prototype.stripOpenComments = function(){
if (this.indexOf('<!--')==-1)
return this.replace(/-->/g, '')
var str='', front=this.indexOf('<!--')<this.indexOf('-->')? 1 : 0, strA=this.split('<!--');
for (var i_tem = 0; i_tem < strA.length; i_tem++){
if ((front||i_tem)&&/-->/.test(strA[i_tem]))
strA[i_tem]='<!--'+strA[i_tem]
else
strA[i_tem]=strA[i_tem].replace(/-->/g, '')
while (/-->.*-->/.test(strA[i_tem]))
strA[i_tem]=strA[i_tem].replace(/-->(?!.*-->)/,'')
str+=strA[i_tem]
}
return str
}
Bookmarks