Lets' say I have this html
code in my String
variable;
String htmlCode = "<span class='test'>test</span>"+
+"<a href=\"http://foo.com?id=<span class='test'>test</span>\">link</a>";
The htmlCode
variable would contain more links similar to that, plus it would also contain more spans
similar to that.
I want to replace everything in between tags <span
and </span>
including those spans, but only if they are in <a href
tag. Meaning that I don't want to replace the first span
tag, but I want to replace the second one.
I know that regex can do that, but so far I was able to do this:
htmlCode = htmlCode.replaceAll("<span.*?</span>", "");
But how do I define that I want to replace it only if it occurs in the <a>
tag? Plus is there a way to replace it including those span
tags?