0

I need to uncomment CSS code inside a style element (and I need to do this in Java). Consider the following html code:

<html>
<head>
<style type="text/css">
<!--
   .big {
      font-size: 30px;
   }
-->
</style>
</head>
<body></body>
</html>

Here is the desired result:

<html>
<head>
<style type="text/css">
   .big {
      font-size: 30px;
   }
</style>
</head>
<body></body>
</html>

I usually use Jericho to do HTML parsing.

UPDATE. Solved:

String newHtmlString = htmlString.replaceAll("<style><!--", "<style>").replaceAll("--></style>", "</style>");
2
  • I assume you mean JavaScript. You can't remove comments, but you can change classes on elements, or add css rules to elements -- which are cleaner options anyway. Commented Mar 31, 2014 at 13:59
  • I have not used Jericho, but I feel the parser cannot help you with this. Because once the parsing is complete, the commented code is ignored already. You may need to do some string manipulation before passing the content through Jericho Commented Mar 31, 2014 at 13:59

1 Answer 1

3

If the only comment in your file is CSS comment, you might consider something like :

    String html = ...; //HTML in String
    html.replaceAll("<!--", "");
    html.replaceAll("-->", "");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.