一个简单的过滤HTM标签的类,谁能帮我解释下

来源:百度知道 编辑:UC知道 时间:2024/05/19 15:15:01
源码如下:
问题现在我不知道repaceEntities是干什么的,而且这个过滤方法也不太好使,谁能帮我改进下!
public static String getTxtWithoutHTMLElement (String element)
{
// String reg="<[^<|^>]+>";
// return element.replaceAll(reg,"");

if(null==element||"".equals(element.trim()))
{
return element;
}

Pattern pattern=Pattern.compile("<[^<|^>]*>");
Matcher matcher=pattern.matcher(element);
StringBuffer txt=new StringBuffer();
while(matcher.find())
{
String group=matcher.group();
if(group.matches("<[\\s]*>"))
{
matcher.appendReplacement(txt,group);
}
else
{
matcher.appendReplacement(txt,"");
}

public static String getTxtWithoutHTMLElement (String element) //静态方法
{
// String reg="<[^<|^>]+>";
// return element.replaceAll(reg,"");

if(null==element||"".equals(element.trim())) //判断element参数是否为空,如果为空直接返回element.即空
{
return element;
}

Pattern pattern=Pattern.compile("<[^<|^>]*>"); //正则表达式
Matcher matcher=pattern.matcher(element); //使用正则表达式验证参数element
StringBuffer txt=new StringBuffer(); //这个就不用解释了
while(matcher.find()) //重置此匹配器,然后尝试查找匹配该模式、从指定索引开始的输入序列的下一个子序列。如果有
{
String group=matcher.group(); //返回由以前匹配操作所匹配的输入子序列
if(group.matches("<[\\s]*>"))//尝试将整个区域与模式匹配。
{
matcher.appendReplacement(txt,group); //替换
}
else
{
matcher.appendReplacement(txt,""); //替换
}
}
matcher.appendTail(txt); //实现终端添加和替换步骤。
repaceEntities(txt,"&","&&quo