Is there the regular expression that can completely remove a HTML tag? By the way, I'm using Java.
There is JSoup which is a java library made for HTML manipulation. Look at the clean()
method and the WhiteList
object. Easy to use solution!
You should use a HTML parser instead. I like htmlCleaner, because it gives me a pretty printed version of the HTML.
With htmlCleaner you can do:
TagNode root = htmlCleaner.clean( stream );
Object[] found = root.evaluateXPath( "//div[id='something']" );
if( found.length > 0 && found instanceof TagNode ) {
((TagNode)found[0]).removeFromTree();
}
If you just need to remove tags then you can use this regular expression:
content = content.replaceAll("<[^>]+>", "");
It will remove only tags, but not other HTML stuff. For more complex things you should use parser.
EDIT: To avoid problems with HTML comments you can do the following:
content = content.replaceAll("<!--.*?-->", "").replaceAll("<[^>]+>", "");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With