1、定义正则表达式:
/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi
2、用正则表达式处理script的方法如下:
<html>
<head>
<!--此处引入script脚本用于测试开始-->
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".btn1").click(function(){
alert($("p").html())
})
})
</script>
<!--此处引入script脚本用于测试结束-->
</head>
<body>
<p>This is a paragraph.</p>
<!--这里增加一个按钮,点击后会删除所有的script块的代码-->
<button class="btn1" onclick="removeAllScript()">删除script</button>
</body>
</html>
<!--定义function处理删除-->
function removeAllScript(obj){
//定义正则表达式,只要是存在于<script>和</script>之间的内容都会被删除
var SCRIPT_REGEX = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi
while (SCRIPT_REGEX.test(obj)) {//传入文档对象,获取整体内容
text = text.replace(SCRIPT_REGEX, "")//正则替换为空
}
}
1、过滤所有html标签的属性的正则表达式:$search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 JavaScript
"'<[\/\!]*?[^<>]*?>'si", // 去掉 HTML 标记
"'([\r\n])[\s]+'",// 去掉空白字符
"'&(quot|#34)'i",// 替换 HTML 实体
"'&(amp|#38)'i",
"'&(lt|#60)'i",
"'&(gt|#62)'i",
"'&(nbsp|#160)'i"
) // 作为 PHP 代码运行
$replace = array ("","","\\1","\"","&","<",">"," ")
$html = preg_replace($search, $replace, $html)
顶
1、正则表达式去掉html标签代码如下:/// <Header>/// 去除 HTML tag
/// </Header>
/// <param name="HTML">源</param>
/// <returns>结果</returns> public static string StripHTML(string HTML) //google "StripHTML" 得到{ string[] Regexs =
{
@"<script[^>]*?>.*?</script>",
@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
@"([\r\n])[\s]+",
@"&(quot|#34)",
@"&(amp|#38)",
@"&(lt|#60)",
@"&(gt|#62)",
@"&(nbsp|#160)",
@"&(iexcl|#161)",
@"&(cent|#162)",
@"&(pound|#163)",
@"&(copy|#169)",
@"(\d+)",
@"-->",
@"<!--.*\n"
}
string[] Replaces =
{
"",
"",
"",
"\"",
"&",
"<",
">",
" ",
"\xa1", //chr(161),"\xa2", //chr(162),"\xa3", //chr(163),"\xa9", //chr(169),"",
"\r\n",
""
}
string s = HTML
for (int i = 0i <Regexs.Lengthi++)
{
s = new Regex(Regexs[i], RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(s, Replaces[i])
}
s.Replace("<", "")
s.Replace(">", "")
s.Replace("\r\n", "")
return s
}
}
2、可以直接复制到txt,然后保存成为.html,在浏览器中设置即可!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)