过滤html
1. java中如何过滤html的代码
||把需要写入数据库的字符通过下面的方法过滤然后再写入回 public static String converthtml(String input) { if (input == null || input.length() == 0) { return input; } StringBuffer buf = new StringBuffer(input.length() + 6); char ch = ' '; for (int i = 0; i < input.length(); i++) { ch = input.charAt(i); if (ch == '&') { buf.append("&"); } else if (ch == '<') { buf.append("<"); } else if (ch == '>') { buf.append(">"); } else if (ch == ' ') { buf.append(""); } else { buf.append(ch); } } return buf.toString(); }
希望采答纳
2. 怎么使用js过滤html标签
你可以利用正则表达式来剔除这些标签,也就是将所有的html类的标签都替换为空即可:
//去除HTML标签
str=str.replace(/</?[^>]*>/g,'');
3. 用正则表达式过滤HTML标签
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* <p>
* Title: HTML相关的正则表达式工具类
* </p>
* <p>
* Description: 包括过滤HTML标记,转换HTML标记,替换特定HTML标记
* </p>
* <p>
* Copyright: Copyright (c) 2006
* </p>
*
* @author hejian
* @version 1.0
* @createtime 2006-10-16
*/
public class HtmlRegexpUtil {
private final static String regxpForHtml = "<([^>]*)>"; // 过滤所有以<开头以>结尾的标签
private final static String regxpForImgTag = "<\\s*img\\s+([^>]*)\\s*>"; // 找出IMG标签
private final static String regxpForImaTagSrcAttrib = "src=\"([^\"]+)\""; // 找出IMG标签的SRC属性
/**
*
*/
public HtmlRegexpUtil() {
// TODO Auto-generated constructor stub
}
/**
*
* 基本功能:替换标记以正常显示
* <p>
*
* @param input
* @return String
*/
public String replaceTag(String input) {
if (!hasSpecialChars(input)) {
return input;
}
StringBuffer filtered = new StringBuffer(input.length());
char c;
for (int i = 0; i <= input.length() - 1; i++) {
c = input.charAt(i);
switch (c) {
case '<':
filtered.append("<");
break;
case '>':
filtered.append(">");
break;
case '"':
filtered.append(""");
break;
case '&':
filtered.append("&");
break;
default:
filtered.append(c);
}
}
return (filtered.toString());
}
/**
*
* 基本功能:判断标记是否存在
* <p>
*
* @param input
* @return boolean
*/
public boolean hasSpecialChars(String input) {
boolean flag = false;
if ((input != null) && (input.length() > 0)) {
char c;
for (int i = 0; i <= input.length() - 1; i++) {
c = input.charAt(i);
switch (c) {
case '>':
flag = true;
break;
case '<':
flag = true;
break;
case '"':
flag = true;
break;
case '&':
flag = true;
break;
}
}
}
return flag;
}
/**
*
* 基本功能:过滤所有以"<"开头以">"结尾的标签
* <p>
*
* @param str
* @return String
*/
public static String filterHtml(String str) {
Pattern pattern = Pattern.compile(regxpForHtml);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result1 = matcher.find();
while (result1) {
matcher.appendReplacement(sb, "");
result1 = matcher.find();
}
matcher.appendTail(sb);
return sb.toString();
}
/**
*
* 基本功能:过滤指定标签
* <p>
*
* @param str
* @param tag
* 指定标签
* @return String
*/
public static String fiterHtmlTag(String str, String tag) {
String regxp = "<\\s*" + tag + "\\s+([^>]*)\\s*>";
Pattern pattern = Pattern.compile(regxp);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result1 = matcher.find();
while (result1) {
matcher.appendReplacement(sb, "");
result1 = matcher.find();
}
matcher.appendTail(sb);
return sb.toString();
}
/**
*
* 基本功能:替换指定的标签
* <p>
*
* @param str
* @param beforeTag
* 要替换的标签
* @param tagAttrib
* 要替换的标签属性值
* @param startTag
* 新标签开始标记
* @param endTag
* 新标签结束标记
* @return String
* @如:替换img标签的src属性值为[img]属性值[/img]
*/
public static String replaceHtmlTag(String str, String beforeTag,
String tagAttrib, String startTag, String endTag) {
String regxpForTag = "<\\s*" + beforeTag + "\\s+([^>]*)\\s*>";
String regxpForTagAttrib = tagAttrib + "=\"([^\"]+)\"";
Pattern patternForTag = Pattern.compile(regxpForTag);
Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib);
Matcher matcherForTag = patternForTag.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result = matcherForTag.find();
while (result) {
StringBuffer sbreplace = new StringBuffer();
Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag
.group(1));
if (matcherForAttrib.find()) {
matcherForAttrib.appendReplacement(sbreplace, startTag
+ matcherForAttrib.group(1) + endTag);
}
matcherForTag.appendReplacement(sb, sbreplace.toString());
result = matcherForTag.find();
}
matcherForTag.appendTail(sb);
return sb.toString();
}
}
4. JS正则过滤指定的HTML标签
1,得到网页上的来链接地址源:
string
matchString =
@"<a[^>]+href=\s*(?:'(?<href>[^']+)'|""(?<href>[^""]+)""|(?<href>[^>\s]+))\s*[^>]*>";
2,得到网页的标题:
string matchString = @"<title>(?<title>.*)</title>";
3,去掉网页中的所有的html标记:
string temp = Regex.Replace(html, "<[^>]*>", ""); //html是一个要去除html标记的文档
4, string matchString = @"<title>([\S\s\t]*?)</title>";
5,js去掉所有html标记的函数:
function delHtmlTag(str)
{
return str.replace(/<[^>]+>/g,"");//去掉所有的html标记
}
5. js如何过滤div内某特定HTML标签
//这里为了方便使用jQuery
//移除使用tag类的div标记下的strong标记下a标记下没有子元素版(链接为权空)的节点元素
jQuery('div.tagstronga:empty').parent().remove();
6. 怎样用js方法过滤html等代码
^<input type="text" id="theOne" value="">
<input type="button" onclick="NoHtml()" value="过滤html标签">
<script>
function NoHtml(){
var t=document.getElementById("theOne").value;
t=t.replace(/({|})/g,''); //过滤{}
t=t.replace(/</g,'<'); //置换符号<
t=t.replace(/>/g,'>'); //置换符号>
// t=t.replace(/<\/?[^>]*>/g,''); //*<\/?[^>]*>可以匹配<script></style></body>等,版并置空权。而不是替换<和>两个符号
document.getElementById("theOne").value=t;
}
</script>
7. 怎么过滤html标签
过滤html标签代码如下:
public string checkStr(string html)
{
System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
html = regex1.Replace(html, ""); //过滤<script></script>标记
html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
html = regex4.Replace(html, ""); //过滤iframe
html = regex5.Replace(html, ""); //过滤frameset
html = regex6.Replace(html, ""); //过滤frameset
html = regex7.Replace(html, ""); //过滤frameset
html = regex8.Replace(html, ""); //过滤frameset
html = regex9.Replace(html, "");
html = html.Replace(" ", "");
html = html.Replace("</strong>", "");
html = html.Replace("<strong>", "");
return html;
}
8. 过滤所有html标签的几种方法
<!DOCTYPE html>
<html lang="en">
<head>
属<meta charset="UTF-8">
<title>test</title>
<script type="text/javascript">
window.onload = function() {
var oTxt1 = document.getElementById('txt1');
var oTxt2 = document.getElementById('txt2');
var test = document.getElementById('test');
test.onclick = function() {
var reg = /<[^<>]+>/g;
oTxt2.value = oTxt1.value.replace(reg, '');
};
}
</script>
</head>
<body>
<div>
<input type="text" id="txt1">
<input type="text" id="txt2">
</div>
<div><button id="test">测试</button></div>
</body>
</html>
9. 急!! 正则表达式过滤html
<%
Function stripHTML(strHTML)
'Strips the HTML tags from strHTML
Dim objRegExp, strOutput
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "<.+?>"
'Replace all HTML tag matches with the empty string
strOutput = objRegExp.Replace(strHTML, "")
'Replace all < and > with < and >
strOutput = Replace(strOutput, "<", "<")
strOutput = Replace(strOutput, ">", ">")
stripHTML = strOutput 'Return the value of strOutput
Set objRegExp = Nothing
End Function
%>
10. js正则表达式过滤html标签,这个正则式怎么写
|代码虽短功能却超强,运行效率也很高!
public static string ClearHtmlCode(string text)
{
text = text.Trim();
if (string.IsNullOrEmpty(text))
return string.Empty;
text = Regex.Replace(text, "[/s]{2,}", " "); //two or more spaces
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|/n)*?>)", " "); //<br>
text = Regex.Replace(text, "(/s*&[n|N][b|B][s|S][p|P];/s*)+", " "); //
text = Regex.Replace(text, "<(.|/n)*?>", string.Empty); //any other tags
text = Regex.Replace(text, "/<//?[^>]*>/g", string.Empty); //any other tags
text = Regex.Replace(text, "/[ | ]* /g", string.Empty); //any other tags
text = text.Replace("'", "''");
text = Regex.Replace(text, "/ [/s| | ]* /g", string.Empty);
return text;
}