2011-04-13 78 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