⑴ 如何用正则表达式 过滤 特定内容
正则表达式:^\d+(\.\d+)?$
你可以用这个正则表达式匹配输入的字符,如果不匹配说明是非法的字母和字符.
⑵ 正则表达式 过滤网址
正则表达式,过滤出所有超链接除了一个url,例如:
<a href= 'http://www.abc.com/'> abc.com </a><br /><a href= 'http://www.edf.com/'> edf.com </a>
过滤:变为abc.com <br /><a href= 'http://www.edf.com/'> edf.com </a>没人知道怎么做么,要保留edf.com的超级链接,过滤掉其他的所有网址的超级链接。
FunctionautoLink(str)
Setra=NewRegExp
ra.IgnoreCase=True
ra.Global=True
ra.Pattern = "<a[^>]+>(.+?)</a>"
autoLink=ra.replace(str,"$1")
ENDFunction
(2)正则表达式过滤扩展阅读:
注意事项:
正则表达式,也称为正则表达式。这是计算机科学中的一个概念。
正则表达式通常用于检索和替换符合模式(规则)的文本,许多编程语言都支持使用正则表达式进行字符串操作。
例如Perl中内置了一个强大的正则表达式引擎。正则表达式的概念最初是由诸如(sed和GREp)这样的Unix工具推广的。
正则表达式通常缩写为“regex”。单数形式是regexp、regex,复数形式是regexps、regexes和regexen。
⑶ 正则表达式如何过滤HTML标签中的属性值
去掉html标签: str.replace(/</?[a-zA-Z]+[^><]*>/g,"")
去掉标签里面的属性: str.replace(/<([a-zA-Z]+)\s*[^><]*>/g,"<$1>")
我亲自测试通过,操作语言专javascript 楼主还有问题的属话Hi 我
⑷ 怎样用正则表达式过滤掉页面中除了<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)
⑸ 正则表达式如何表示若干个空格我想用正则表达式过滤掉空字符串,用“”方法没用,求解。如果一段文本是
s*表示若干个空格(可以是0个)。
s+ 表示一个或多个空格
publicclassTest{
publicstaticvoidmain(String[]args){
Stringstr="";
//测试的字符串
Stringregex="\s+";
//表示一个或多个空格的正则表达式
str=str.trim();
//去掉字符串开头和结尾的空格
Stringstr1=str.replaceAll(regex,"");
//去掉所有的空格
Stringstr2=str.replaceAll(regex,"");
//把一个或多个空格替换成一个空格
System.out.println(str);
System.out.println(str1);
System.out.println(str2);
}
}
输出结果如下:
⑹ 正则表达式过滤'_'下划线。
这么写就行了 不过有这个必要吗
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正则表达式过滤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;
}
⑻ 正则表达式过滤中文
/^(^([\\u4E00-\\u9FA5]|[\\uFE30-\\uFFA0]))*$/
你是要这个吧?
^在
正则表达式
中,还有字符串开始的意思....
⑼ 如何用用正则表达式过滤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, ""); //正则替换为空
}
}
⑽ 正则表达式过滤特殊字符
正则表达式里面你带了逗号,应该这样写
[。~!@#$%\^\+\*&\\\/\?\|:\.<>{}()';="]
有些符号只有少数几个符号需要转义,而且不用打逗号,打了逗号就相当于把逗号也过滤掉了