Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing italic and bold html tags from string?

Tags:

java

html

regex

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,.)?

like image 765
genxgeek Avatar asked Dec 26 '22 14:12

genxgeek


1 Answers

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

enter image description here

like image 62
Sujith PS Avatar answered Jan 09 '23 19:01

Sujith PS