What is a safer way to remove bold and italics than the following?
String text = "<b>Remove <i>bold</i> and italics</b>";
System.out.println(text);
text = text.replaceAll("\\<.*?\\>", ""); //remove all but only want to remove b and i?
System.out.println(text);
Also, and more extensible (if I want to include other tags such as "strong" or "em" and allow for case sensitivity "b" vs "B" etc,.)?
You can use this regex :<\/?[bi]>
DEMO
CODE :
String text = "<b>Remove <i>bold</i> and italics</b>";
text = text.replaceAll("<\\/?[bi]>", "");
System.out.println(text);
OUTPUT
Remove bold and italics
If you want to match case insensitive then you can use corresponding flag (?i)
EXPLANATION
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