Java去除基本的HTML標籤
阿新 • • 發佈:2018-12-14
// 去除基本的標籤 public static String replaceHtmlBasicTag(String content, String replacement) { String rtn = content; if (StringUtils.isNotBlank(rtn) && replacement != null) { Matcher htmlTag = Pattern.compile("<script [^>]*>(.*?)</script>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.COMMENTS).matcher(rtn); if (htmlTag.find()) { rtn = htmlTag.replaceAll(replacement); } htmlTag = Pattern.compile("<style [^>]*>(.*?)</style>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.COMMENTS).matcher(rtn); if (htmlTag.find()) { rtn = htmlTag.replaceAll(replacement); } htmlTag = Pattern.compile("<link [^>]*>(.*?)</link>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.COMMENTS).matcher(rtn); if (htmlTag.find()) { rtn = htmlTag.replaceAll(replacement); } htmlTag = Pattern.compile("<([^>]*)>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE).matcher(rtn); if (htmlTag.find()) { rtn = htmlTag.replaceAll(replacement); } rtn = rtn.replaceAll(" +", " ").replaceAll("\\t+", " ").replaceAll(" +", " ").replaceAll("( ){1,}", " ").replace("|0", ""); if(rtn.contains("您的瀏覽器不支援video標籤。")){ rtn = rtn.replaceAll("您的瀏覽器不支援video標籤。", ""); } if (rtn.contains("您不支援音訊播放")) { rtn = rtn.replaceAll("您不支援音訊播放", ""); } if (rtn.contains("您不支援視訊播放")) { rtn = rtn.replaceAll("您不支援視訊播放", ""); } if (rtn.contains("您的瀏覽器不支援此視訊格式")) { rtn = rtn.replaceAll("您的瀏覽器不支援此視訊格式", ""); } } return rtn; }