㈠ 怎样用正则表达式过滤掉页面中除了<p></p>和<img>以外所有的标签
这个还真不容易实现,单独保留p或者img都可以,但是两个条件放一起就不行了。于专是我换属了一种思路,用了个函数实现了,你看下,代码是python下的:
importre
t='<html>asdfasdf<head>1111111111<body><p>asdfasdfasdf</p><imgherf="fff">'
defreplace_two(m):
"""
#过滤掉页面中除了<p></p>和<img>以外所有的标签
"""
all=re.findall(r'</?.*?>',m)
save=re.findall(r'</?(?:img).*?>|</?[pP]*?>',m)
foreinall:
ifenotinsave:
m1=m.replace(e,'')
m=m1
returnm
printreplace_two(t)
㈡ 正则表达式过滤'_'下划线。
这么写就行了 不过有这个必要吗
using System.Text.RegularExpressions;
string[] strArr = new string[] {
"aaa.kels_kwoo._lwie" ,
"aaa.kels kwoo.lwie",
"kels kwoo.lwie",
"kels kwoo._lwie"
};
Regex regex = new Regex("^[^_]+$");
foreach (string item in strArr)
{
if (regex.IsMatch(item))
{
Console.WriteLine(item);
}
}
Console.ReadKey();
㈢ Js字符串的正则匹配 如何过滤掉指定特征的字符串
String.replace(正则表达式,"")
replace是string类型内置的替换方法,第一个参数可以是正则表达式,第二个版参数是想权要替换成的文本,正则中可以使用/g来表示替换所有匹配的文本,不使用则代表只替换匹配到的第一个字符对象,将第二个参数设为空字符串便可达到过滤的效果。
具体正则需要你自己去了解关于正则的知识了,祝你好运。
㈣ 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|][r|R]/*>)+|(<[p|P](.|/n)*?>)",
"
");
//
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;
}
㈤ js中用正则表达式 过滤特殊字符 校验所有输入域是否含有特殊符号
楼上2位已经说的很明白了,只允许输入规定的字符,如果输入含有其他字符就直接提示,不允许输入特殊字符,或者直接给它替换掉。
㈥ 正则表达式获取span中的值
^内容<input type="text" id="text" value="<span id=>e</span>"/><br/>
<input type="button" onclick="if(/<span[^>]*>[^<]*?<\/span>/.test(text.value)){alert(text.value.match(/<span[^>]*>[^<]*?<\/span>/))}else{alert('匹配不到结果!')}" value="正则表回达式验证答" /><br/>
㈦ 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;
}
㈧ 在javascript中用正则表达式过滤指定的字符(一定要能指定!)
楼上的不加转义字符\ 你们搞什么啊
正确的应该是这样的
加入你得到的内字符窜容为 name
<html>
<head>
<script>
function test1(){
var name=document.getElementById('user').value;
name=name.replace(/(\!+)|(\<+)|(\>+)|(\'+)/g,"");
alert(name);
}
</script>
</head>
<body>
<input type="text" id="user" />
<input type="button" value="te" onclick="test1()">
</body>
</html>
㈨ 如何用用正则表达式过滤html中所有 Script
用正则表达式过滤html中所有 的方法:
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, ""); //正则替换为空
}
}