Removing italic and bold html tags from string?

html, java, regex

Solution

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

Problem

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

Original source