增加HtmlUtil.cleanEmptyTag方法(pr#3838@Github)

This commit is contained in:
Looly
2025-01-10 09:58:35 +08:00
parent b8ba60df50
commit 60b1413aa6
2 changed files with 79 additions and 40 deletions

View File

@@ -41,6 +41,10 @@ public class HtmlUtil {
* HTML标签正则
*/
public static final Pattern RE_HTML_MARK = Pattern.compile("(<[^<]*?>)|(<\\s*?/[^<]*?>)|(<[^<]*?/\\s*?>)", Pattern.CASE_INSENSITIVE);
/**
* 正则:匹配空标签
*/
public static final String RE_HTML_EMPTY_MARK = "<(\\w+)([^>]*)>\\s*</\\1>";
/**
* script标签正则
*/
@@ -111,6 +115,17 @@ public class HtmlUtil {
return ReUtil.replaceAll(content, RE_HTML_MARK, StrUtil.EMPTY);
}
/**
* 清除所有HTML空标签<br>
* 例如:{@code <p></p>}
*
* @param content 文本
* @return 清除空标签后的文本
*/
public static String cleanEmptyTag(final String content) {
return content.replaceAll(RE_HTML_EMPTY_MARK, StrUtil.EMPTY);
}
/**
* 清除所有script标签包括内容
*