Quantcast
Viewing latest article 9
Browse Latest Browse All 19

正则过滤HTML代码,并保留自定义标签(附JAVA代码)

public class test {

    public static void main(String[] args) {

        String content;

        content = "<img>image_url</font>";

        System.out.println(stripTags(content));
    }

    public static String stripTags(String content) {

        if (content == null)
            return null;

        content = content.replaceAll("<(?!/?(?i)(img|p|br|font)).*?>", "");
        return content;
    }
}

 

注释:

content.replaceAll("<" + //开头的 '<'
                "(?!" + //正向预搜索否定
                "/?" + // 0或1次 '/'
                "(?i)" + //IGNORECASE
                "(img|p|br|font))" + //支持的标签
                ".*?" + //匹配剩下的 勉强模式
                ">" //最后的 '>'
                , "");


Viewing latest article 9
Browse Latest Browse All 19

Trending Articles