Remove HTML remove aligment using Java

229 Views Asked by At

I Have faces a issue with removeing alignment In HTML document.

 <html>
  <head>

  </head>
  <body>
    <p style="margin-top: 0" align="center">
      Hello World
    </p>
    <p style="margin-top: 0" align="center">
      Java World
    </p>
  </body>
</html>

My Issue is how to remove alignment of first paragraph with out affecting second paragraph . If I use regex it will it will remove alignment of second para also. I really appricite you any comment regarding this issue.

2

There are 2 best solutions below

0
On

Use the replaceFirst function.

1
On

I'd like to show you another way for that. It would be quite simple to use CSS pseudo-class: :first-child. According to your code above:


body p:first-child { text-align: left !important; }

Second thing would be to use JavaScript or any JS library, like jQuery to remove this property from first p element, e.g.:


$(document).ready(function(){
    $("p").first().css("text-align","left");
});