如何删除 HTML 字符串中的所有注释

png*_*png 0 java regex string android

我有一个ieString中有评论。我想删除所有这些。<! --><!comment1-->

RE 是什么?

我试过:

replaceAll("\\<!.*?\\-\\-\\>", "");
Run Code Online (Sandbox Code Playgroud)

但这没有用。我尝试循环和替换并且它有效,但我正在寻找正则表达式

我已经尝试过该墨水中提到的 html.fromHtml,但它不起作用。为此我在这里提出了另一个问题

例如下面的字符串

<style> <!-- /* Font Definitions */ @font-face  {font-family:"Cambria Math";    panose-1:2 4 5 3 5 4 6 3 2 4;} @font-face       {font-family:Calibri;   panose-1:2 15 5 2 2 2 4 3 2 4;} @font-face      {font-family:Tahoma;    panose-1:2 11 6 4 3 5 4 4 2 4;} @font-face      {font-family:Webdings;  panose-1:5 3 1 2 1 5 9 6 7 3;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal         {margin:0in;    margin-bottom:.0001pt;  font-size:11.0pt;       font-family:"Calibri",sans-serif;} a:link, span.MsoHyperlink    {mso-style-priority:99;         color:#0563C1;  text-decoration:underline;} a:visited, span.MsoHyperlinkFollowed        {mso-style-priority:99;         color:#954F72;  text-decoration:underline;} span.EmailStyle17   {mso-style-type:personal-compose;       font-family:"Calibri",sans-serif;       color:windowtext;} .MsoChpDefault       {mso-style-type:export-only;    font-family:"Calibri",sans-serif;} @page WordSection1   {size:8.5in 11.0in;     margin:1.0in 1.0in 1.0in 1.0in;} div.WordSection1       {page:WordSection1;} --></style>
Run Code Online (Sandbox Code Playgroud)

hat*_*ata 5

确保您收到来自的返回值String.replaceAll

String html = "abcde<!--comment1-->\n"
        + "<p>abcdefghi<!--comment2-->jkl</p>\n"
        + "<p>abcdefghi<span>jklm<!--comment3-->nopq</span>rs</p>\n";

String commentsRemoved = html.replaceAll("<!--.*?-->", "");

System.out.println(html);
System.out.println(commentsRemoved);
Run Code Online (Sandbox Code Playgroud)