String str = "<script type=\"text/javascript\">";
str+="document.write('<a style=\"display:none!important\" id=\"tanx-a-mm_114791679_13280975_57892040\"></a>');";
str+="tanx_s = document.createElement(\"script\");";
str+="tanx_s.type = \"text/javascript\";";
str+="tanx_s.charset = \"gbk\";";
str+="tanx_s.id = \"tanx-s-mm_114791679_13280975_57892040\";";
str+="tanx_s.async = true;";
str+="tanx_s.src = \"http://p.tanx.com/ex?i=mm_114791679_13280975_57892040\";";
str+="tanx_h = document.getElementsByTagName(\"head\")[0];";
str+="if(tanx_h)tanx_h.insertBefore(tanx_s,tanx_h.firstChild);";
str+="</script>";
str+="<p>12312312</p>";
str+="<script></script>";
String tempsCon_ = str.replaceAll("<script[^>]*>((?!</script>).)*</script>","");
System.out.println("tempsCon_=>"+tempsCon_);//输出结果<p>12312312</p>
代码如上,在工作中遇到一个问题,需要将抓取到的html页面中的script代码、htmp标签过滤掉,发现如上图字符串那样的js定义,找了些资料。可以使用
<script[^>]*>((?!</script>).)*</script>过滤所有写在页面内的js脚本。
参考文章:http://blog.youkuaiyun.com/wang8651971/article/details/45207863