过滤html标 大家好,我想问一下,如何能将当前的页的html标签过滤掉,但还要求保留input的value值,例如说:问题:<textarea name="textfield23" cols="80" rows="8">测试</textarea>最后就变成->问题:测试 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 去掉所有的<,>以及之间的东西就行了吧,但前提是页面代码必须规范 通过正则表达式,把<和>之间的东西替换为空字符串就行了。 public static String html2Text(String inputString) { String htmlStr = inputString; //含html标签的字符串 String textStr =""; java.util.regex.Pattern p_script; java.util.regex.Matcher m_script; java.util.regex.Pattern p_style; java.util.regex.Matcher m_style; java.util.regex.Pattern p_html; java.util.regex.Matcher m_html; try { String regEx_script ="<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定义script的正则表达式{或<script[^>]*?>[\s\S]*?<\/script> } String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义style的正则表达式{或<style[^>]*?>[\s\S]*?<\/style> } String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式 p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); m_script = p_script.matcher(htmlStr); htmlStr = m_script.replaceAll(""); //过滤script标签 p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); m_style = p_style.matcher(htmlStr); htmlStr = m_style.replaceAll(""); //过滤style标签 p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); m_html = p_html.matcher(htmlStr); htmlStr = m_html.replaceAll(""); //过滤html标签 textStr = htmlStr; }catch(Exception e) { System.err.println("Html2Text: " + e.getMessage()); } return textStr;//返回文本字符串 } 你这个会把我的input 中的value的值滤掉的,所以不行。嘿嘿,有没有高招啊,大家看来都不用这个啊,唉。我这个问题怎么这么怪呢! 不行的,我想到了一个方法,但也只是要在一定的条件下才能行,我放出来,大家看看,希望大家给出新的想法.function showText(sqart){ var text = ""; for(var i=0;i<document.body.childNodes.length;i++) { var n = document.body.childNodes[i]; if(n.tagName == 'SPAN'){ text += n.innerHTML; } if(n.tagName == 'INPUT'&&n.type == 'text'){ text += n.value + sqart; } if(n.tagName == 'TEXTAREA'){ text += n.value + sqart; } } document.getElementById('text').value = text; document.form1.submit();}我现在写的这个东西只能过滤几种标签,希望大家能给出更好的解决方案 关于twitter的问题 想做一个网页报纸 如何解决new Socket( host, port )引起的阻塞? 这样调用对不对 URL重定向后,如何知道重定向到哪个地址? JSP下获取用户IP? javamail的一个奇怪问题 大牛帮个忙!!万分感谢 请问哪里有网上购物系统(如网上书店)的jsp源代码下载? 一个关于在win2000下安装jsp的问题?求助各位兄台!!!!!!!!!!! actionForm 中List模型怎么建? 关于 LINUX下 JAVA使用JNI调用C.SO动态库问题 。。。。。
String htmlStr = inputString; //含html标签的字符串
String textStr ="";
java.util.regex.Pattern p_script;
java.util.regex.Matcher m_script;
java.util.regex.Pattern p_style;
java.util.regex.Matcher m_style;
java.util.regex.Pattern p_html;
java.util.regex.Matcher m_html; try {
String regEx_script ="<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定义script的正则表达式{或<script[^>]*?>[\s\S]*?<\/script> }
String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义style的正则表达式{或<style[^>]*?>[\s\S]*?<\/style> }
String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式 p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); //过滤script标签 p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); //过滤style标签 p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); //过滤html标签 textStr = htmlStr;
}catch(Exception e) {
System.err.println("Html2Text: " + e.getMessage());
} return textStr;//返回文本字符串
}
function showText(sqart)
{
var text = "";
for(var i=0;i<document.body.childNodes.length;i++)
{
var n = document.body.childNodes[i];
if(n.tagName == 'SPAN'){
text += n.innerHTML;
}
if(n.tagName == 'INPUT'&&n.type == 'text'){
text += n.value + sqart;
}
if(n.tagName == 'TEXTAREA'){
text += n.value + sqart;
}
}
document.getElementById('text').value = text;
document.form1.submit();
}
我现在写的这个东西只能过滤几种标签,希望大家能给出更好的解决方案