Regex to replace html heading elements

In this example we will use a regular expression to replace html heading elements. We showed a similiar example in the how to modify html response tutorial. Below we defined a snippet of html with <h2> and wrote a regular expression to replace it with a new html tag. The <h2> could be replaced with a <h1>, <h3> <h4> or <h5>.

public void replace_heading_elements() {

    String htmlContent = "<h2>Replace html heading</h2>";

    String h2Toh3 = htmlContent.replaceAll("<h2[^>]*>(.*?)</h2>",
            "<h3>$1 - HTML replaced</h3>");

    assertEquals("<h3>Replace html heading - HTML replaced</h3>", h2Toh3);