Ⅰ 如何使用js正则 过滤某一个html标签下所有的标签跟样式呢只保留出纯文本
js过滤标签的方法。分享给大家供大家参考,具体如下:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>无标题文档</title>
<script>
window.onload=function()
{
varoTxt1=document.getElementById('txt1');
varoTxt2=document.getElementById('txt2');
varoBtn=document.getElementById('btn');
oBtn.onclick=function()
{
varreg=/<[^<>]+>/g;
oTxt2.value=oTxt1.value.replace(reg,'');
};
};
</script>
</head>
<body>
<textareaid="txt1"cols="40"rows="10"></textarea><br/>
<inputtype="button"value="过滤"id="btn"/><br/>
<textareaid="txt2"cols="40"rows="10"></textarea>
</body>
</html>
Ⅱ 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正则过滤以下代码
varhtml='<p><spanstyle="text-indent:43px;font-size:21px;font-family:宋体;">11</span><spanstyle="text-indent:43px;font-size:21px;font-family:宋体;">月<span>26</span>日,党委副书记带领学校部分获得教学能手的教师到初级中学,学生的生活、学习送去“温暖”。</span></p> <pstyle="text-indent:43px;mso-char-indent-count:2.0"><spanstyle="font-size:21px;font-family:宋体;">为帮助初中学生抵御严寒,捐赠<span>5000</span>元现金,希望他们能在寒冷的冬季克服困难,认真踏实学习。</span></p> <pstyle="text-indent:43px;text-align:center;"><imgsrc="/uploadfiles/CMS/image/1448529129738_0.jpg"width="665"height="500"alt=""/></p> <pstyle="text-indent:43px;text-align:center;"><imgsrc="/uploadfiles/CMS/image/1448529242675_0.jpg"width="665"height="500"alt=""/></p> <pstyle="text-indent:43px;mso-char-indent-count:2.0"><spanstyle="font-size:21px;font-family:宋体;mso-ascii-theme-font:major-fareast; mso-fareast-theme-font:major-fareast;mso-hansi-theme-font:major-fareast"><o:p></o:p></span></p>';
varreg=/</?(?:(?!p|img)[^>]*)>/gi;
html=html.replace(reg,"");
console.log(html);
Ⅳ 怎样用正则表达式过滤掉页面中除了<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)
Ⅵ 在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>
Ⅶ 正则js过滤除p以外标签
是连p以外的标签的内容都不要么?
如果是的话,可以反过来想,只要匹配所有p标签以及其以内的内容即可,那么可以这么写
varre=newRegExp("<p>\w*</p>","g");
result=str.match(re)
str是你所需要进行匹配的字符串,如果是页面全部,就获取body下所有的内容就好
result是一个数组,而每一个元素就是你匹配对象中的p标签以及其内容
Ⅷ 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标记
}
Ⅸ js 正则过滤特殊字符
您好
js检查是否含有非法字符,js 正则过滤特殊字符
//正则
functiontrimTxt(txt){
returntxt.replace(/(^s*)|(s*$)/g,"");
}
/**
*检查是否含有非法字符
*@paramtemp_str
*@returns{Boolean}
*/
functionis_forbid(temp_str){
temp_str=trimTxt(temp_str);
temp_str=temp_str.replace('*',"@");
temp_str=temp_str.replace('--',"@");
temp_str=temp_str.replace('/',"@");
temp_str=temp_str.replace('+',"@");
temp_str=temp_str.replace(''',"@");
temp_str=temp_str.replace('\',"@");
temp_str=temp_str.replace('$',"@");
temp_str=temp_str.replace('^',"@");
temp_str=temp_str.replace('.',"@");
temp_str=temp_str.replace(';',"@");
temp_str=temp_str.replace('<',"@");
temp_str=temp_str.replace('>',"@");
temp_str=temp_str.replace('"',"@");
temp_str=temp_str.replace('=',"@");
temp_str=temp_str.replace('{',"@");
temp_str=temp_str.replace('}',"@");
varforbid_str=newString('@,%,~,&');
varforbid_array=newArray();
forbid_array=forbid_str.split(',');
for(i=0;i<forbid_array.length;i++){
if(temp_str.search(newRegExp(forbid_array[i]))!=-1)
returnfalse;
}
returntrue;
}
---------------------
作者:dongsir 董先生
来源:董先生的博客
原文链接:js检查是否含有非法字符
版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。转载时请标注:http://dongsir.cn/p/195
Ⅹ 怎么用js或者jQuery去除掉某个标签但是不去除里面的内容
1、可以使用正则直接取到img
//思路分两步:
//1,匹配出图片img标签(即匹配出所有图片),过滤其他不需要的字符
//2.从匹配出来的结果(img标签中)循环匹配出图片地址(即src属性)
varstr="<td>thisisteststring<imgsrc="http:yourweb.com/test.jpg"width='50'>123andtheend<imgsrc="所有地址也能匹配.jpg"/>33!<imgsrc="/uploads/attached/image/20120426/20120426225658_92565.png"alt=""/></td>"
//匹配图片(g表示匹配所有结果i表示区分大小写)
varimgReg=/<img.*?(?:>|/>)/gi;
//匹配src属性
varsrcReg=/src=['"]?([^'"]*)['"]?/i;
vararr=str.match(imgReg);
alert('所有已成功匹配图片的数组:'+arr);
for(vari=0;i<arr.length;i++){
varsrc=arr[i].match(srcReg);
//获取图片地址
if(src[1]){
alert('已匹配的图片地址'+(i+1)+':'+src[1]);
}
//当然你也可以替换src属性
if(src[0]){
vart=src[0].replace(/src/i,"href");
//alert(t);
}
}