我想 过虑掉所有HTML的标记 只显示内容 比如:
"<div onMouseOver=this.className='onselect' onMouseOut=this.className='' onDblClick='document.all.msg.value=document.all.msg.value+this.innerText'><font color=#ff0000>[Fri Sep 01 17:01:40 CST 2006]客服<br><font face='宋体' color=' 'style='font-size: px;line-height:10px'>你好,我是客服</font></font></div>"只显示 "客服 你好,我是客服"我找了好多JAVASCRIPT和VBSCRIPT的方法 都是用正则表达式 觉得都不好用 请问有没有用JAVA实现的方法啊,谢谢.
"<div onMouseOver=this.className='onselect' onMouseOut=this.className='' onDblClick='document.all.msg.value=document.all.msg.value+this.innerText'><font color=#ff0000>[Fri Sep 01 17:01:40 CST 2006]客服<br><font face='宋体' color=' 'style='font-size: px;line-height:10px'>你好,我是客服</font></font></div>"只显示 "客服 你好,我是客服"我找了好多JAVASCRIPT和VBSCRIPT的方法 都是用正则表达式 觉得都不好用 请问有没有用JAVA实现的方法啊,谢谢.
str=str.replaceAll("<[^<>]+>","");
直接使用正则替换即可
我用了你们的方法 可是我传进
"<div onMouseOver=this.className='onselect' onMouseOut=this.className='' onDblClick='document.all.msg.value=document.all.msg.value+this.innerText'><font color=#ff0000>[Fri Sep 01 17:01:40 CST 2006]客服<br><font face='宋体' color=' 'style='font-size: px;line-height:10px'>你好,我是客服</font></font></div>"这个参数 输出什么都没有啦啊 请问是为啥??
str=str.replaceAll("<[^<>]+>","");
经测试以上代码完全正确,测试输出结果:
[Fri Sep 01 17:01:40 CST 2006]客服你好,我是客服
public static void main(String[] args) {
String str = "<div onMouseOver=this.className='onselect' onMouseOut=this.className='' onDblClick='document.all.msg.value=document.all.msg.value+this.innerText'><font color=#ff0000>[Fri Sep 01 17:01:40 CST 2006]客服<br><font face='宋体' color=' 'style='font-size: px;line-height:10px'>你好,我是客服</font></font></div>";
System.out.println(str.replaceAll("<[^>]*>",""));
}
}
---------- Run ----------
[Fri Sep 01 17:01:40 CST 2006]客服你好,我是客服输出完成 (耗时: 0 秒) - 正常终止
呵呵,两次都是发出来看到你的贴在上面
lip009(深蓝忧郁)
能把你测试的代码放上来么 我这里怎么死活都是空的啊我这么写不对么??
public class test {
public String htmlFilter(String inputString) {
String str = "";
str=str.replaceAll("<[^<>]+>","");
return str;
}
public static void main(String a[]){
test aa = new test();
System.out.println(aa.htmlFilter("<div onMouseOver=this.className='onselect' onMouseOut=this.className='' onDblClick='document.all.msg.value=document.all.msg.value+this.innerText'><font color=#ff0000>[Fri Sep 01 17:01:40 CST 2006] mahe<br><font face='宋体' color=' 'style='font-size: px;line-height:10px'>33333333333</font></font></div>"));
}
}
嘿嘿...to: msdnweixiaomsdn(微笑) ( ) 信誉:100 Blog 把这行String str = "";改成
String str=inputString;