Remove HTML tags from a String

后端 未结 30 3106
误落风尘
误落风尘 2020-11-21 07:35

Is there a good way to remove HTML from a Java string? A simple regex like

replaceAll("\\\\<.*?>", &quo         


        
30条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 07:39

    I often find that I only need to strip out comments and script elements. This has worked reliably for me for 15 years and can easily be extended to handle any element name in HTML or XML:

    // delete all comments
    response = response.replaceAll("", "");
    // delete all script elements
    response = response.replaceAll("<(script|SCRIPT)[^+]*?>[^>]*?<(/script|SCRIPT)>", "");
    

提交回复
热议问题