Remove HTML tags from a String

后端 未结 30 3088
误落风尘
误落风尘 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:38

    I think that the simpliest way to filter the html tags is:

    private static final Pattern REMOVE_TAGS = Pattern.compile("<.+?>");
    
    public static String removeTags(String string) {
        if (string == null || string.length() == 0) {
            return string;
        }
    
        Matcher m = REMOVE_TAGS.matcher(string);
        return m.replaceAll("");
    }
    

提交回复
热议问题