2011-04-13 87 views
36

我从Word生成了一些难看的HTML,我想从中删除所有HTML注释。使用正则表达式在Javascript中删除HTML评论

的HTML看起来像这样:

<!--[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]--> 

..和我使用的正则表达式是这个

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

但似乎没有比赛,该字符串是不变的。

我缺少什么?

+4

适合我。检查http://jsfiddle.net/aQ5qp/ – Chandu 2011-04-13 17:37:27

+0

整个字符串是一个评论,因此一切都被替换为“” – Chandu 2011-04-13 17:42:30

+1

正如@Cyber​​nate说,正则表达式* * *工作在该文本,所以什么给?所有响应者都假设文本中有新行,这将解释问题,但我没有看到任何换行符。 – 2011-04-13 21:42:55

回答

62

正则表达式/<!--[\s\S]*?-->/g应该工作。

你要杀死CDATA块中的escaping text spans

例如

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

和格式化代码块

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

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

编辑:

这也不会阻止新的评论被引入作为

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

其中一个后该正则表达式的一轮将变为

<!-- not comment text --> 

如果这是一个问题,你可以转义<不属于评论或标签(复杂得到正确),或者你可以循环和替换如上,直到字符串安定下来。


这里有一个正则表达式,将匹配的意见,包括每HTML-5规格psuedo-comments和结束的注释。 CDATA部分只能在外部XML中使用。这受到与上述相同的警告。

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'); 
+0

你会如何修改此仅获取评论,并删除html? – guiomie 2013-07-25 18:28:13

+0

@guiomie,我不明白你的目标。请详细解释一下? – 2013-07-25 20:00:36

+1

@MikeSamuel你最后的代码片段错过了CDATA周围的两个反斜杠转义。 – 2016-02-29 17:15:48

0
html = html.replace("(?s)<!--\\[if(.*?)\\[endif\\] *-->", "") 
+1

JavaScript不支持's'修饰符,也不支持内联修饰符语法,像'(?s)'。 – 2011-04-13 21:06:14

3

您应该使用/s修改

HTML = html.replace(/<!--.*?-->/sg, “”)

测试在Perl:

use strict; 
use warnings; 

my $str = 'hello <!--[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]-->world!'; 

$str =~ s/<!--.*?-->//sg; 
print $str; 

输出:
hello world!

+3

JavaScript没有's'修饰符。使用'[\ s \ S]'而不是'.'。 – 2011-04-13 18:45:09

+0

谢谢。我觉得这很惊人。 – sln 2011-04-14 01:37:20