Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  85] [ 4]  / answers: 1 / hits: 44228  / 13 Years ago, wed, april 13, 2011, 12:00:00

I've got some ugly HTML generated from Word, from which I want to strip all HTML comments.



The HTML looks like this:



<!--[if gte mso 9]><xml> <o:OfficeDocumentSettings> <o:RelyOnVML/> <o:AllowPNG/> </o:OfficeDocumentSettings> </xml><![endif]--><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:TrackMoves/> <w:TrackFormatting/> <w:HyphenationZone>21</w:HyphenationZone> <w:PunctuationKerning/> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:DoNotPromoteQF/> <w:LidThemeOther>NO-BOK</w:LidThemeOther> <w:LidThemeAsian>X-NONE</w:LidThemeAsian> <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript> <w:Compatibility> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:SplitPgBreakAndParaMark/> <w:EnableOpenTypeKerning/> <w:DontFlipMirrorIndents/> <w:OverrideTableStyleHps/> </w:Compatibility> <m:mathPr> <m:mathFont m:val=Cambria Math/> <m:brkBin m:val=before/> <m:brkBinSub m:val=&#45;-/> <m:smallFrac m:val=off/> <m:dispDef/> <m:lMargin m:val=0/> <m:rMargin m:val=0/> <m:defJc m:val=centerGroup/> <m:wrapIndent m:val=1440/> <m:intLim m:val=subSup/> <m:naryLim m:val=undOvr/> </m:mathPr></w:WordDocument> </xml><![endif]-->


..and the regex I am using is this one



html = html.replace(/<!--(.*?)-->/gm, )


But there seems to be no match, the string is unchanged.



What I am missing?


More From » regex

 Answers
57

The regex /<!--[sS]*?-->/g should work.



You're going to kill escaping text spans in CDATA blocks.



E.g.



<script><!-- notACommentHere() --></script>


and literal text in formatted code blocks



<xmp>I'm demoing HTML <!-- comments --></xmp>

<textarea><!-- Not a comment either --></textarea>


EDIT:



This also won't prevent new comments from being introduced as in



<!-<!-- A comment -->- not comment text -->


which after one round of that regexp would become



<!-- not comment text -->


If this is a problem, you can escape < that are not part of a comment or tag (complicated to get right) or you can loop and replace as above until the string settles down.






Here's a regex that will match comments including psuedo-comments and unclosed comments per the HTML-5 spec. The CDATA section are only strictly allowed in foreign XML. This suffers the same caveats as above.



var COMMENT_PSEUDO_COMMENT_OR_LT_BANG = new RegExp(
'<!--[\s\S]*?(?:-->)?'
+ '<!---+>?' // A comment with no body
+ '|<!(?![dD][oO][cC][tT][yY][pP][eE]|\[CDATA\[)[^>]*>?'
+ '|<[?][^>]*>?', // A pseudo-comment
'g');

[#92748] Tuesday, April 12, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
angelicajayleneh

Total Points: 216
Total Questions: 110
Total Answers: 100

Location: Sudan
Member since Tue, Aug 3, 2021
3 Years ago
;