Home > OS >  How to escape the html tag if its closing tag is not found in a string using java?
How to escape the html tag if its closing tag is not found in a string using java?

Time:09-28

I have a string like below

"Its a great <strong><b>job</strong>".

I need to get the output like below

"Its a great <strong>&lt;b&gt;job</strong>". 

It might be any html tag. if the html tag is not having closing tag then I need to escape it using StringEscapeUtils.escapeHtml(). so my main task is to find the unclosed html tags in a particular string

CodePudding user response:

I recommend using String.replaceAll(regex, replacement).

    String str = "Its a great <strong><b>job</strong>";
    str = str.replaceAll("<b>", "&lt;b&gt;");

CodePudding user response:

String s = "Its a great <strong><b>job</strong>";

Map<Integer, Integer> indexes = new HashMap();

Pattern p = Pattern.compile("<(\w)*>([^<])*</(\w)>");
Matcher m = p.matcher(s);

int opening = -1;
int closing = -1;

while (m.find()) {
    if (!m.group(0).equals(m.group(2))) {
        opening = m.start(1);
        closing = m.group(1).indexOf('>');
    }

    if (opening != -1 && closing != -1) {
        indexes.put(opening, closing);
    }
}

Now you have a map with all of the invalid xml tags' indexes so that you can manipulate them.

  • Related