去除图片超级链接,垃圾代码,正则表达式? 上问写错了替换后干净的代码: <IMG src="http://img.qihoo.com/qhimg/kkk.gif" > 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <script language="javascript"> str = '<A href="http://rd.union.qihoo.com/ target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/kkk.gif" width=70 border=0 onload="javascript:if(this.width>120)this.width=120;if(this.height>120)this.height=120;"></A>'str = str.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)>(.*?)$/i,"<img src=\"$2\">")alert(str);</script> 回上楼,如果内容变成这样,就失效了<A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FMAXBreast%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=865798593&t=1204978972&e=5375debee73a5972af40b3daa0e761d1&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/576/851cb2.gif" width=70 border=0></A>不能替换成这样的干净代码<IMG src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/576/851cb2.gif"> <script language="javascript"> str = '<A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FMAXBreast%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=865798593&t=1204978972&e=5375debee73a5972af40b3daa0e761d1&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/576/851cb2.gif" width=70 border=0></A>' //str = str.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)>(.*?)$/i," <img src=\"$2\"> ") str = str.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)>(.*?)$/i," <img src=\"$2\"> ") alert(str); </script> 你以上写法是对的,但这样加个表单就不行了!帮忙看一下!<script>function Replace(){ var ta=document.getElementById("ta"); ta.value= ta.value.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)> (.*?)$/i," <img src=\"$2\">") }</script><body> <textarea style="width:400px;height:120px" id="ta"><A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FMAXBreast%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=681805886&t=1204983753&e=811982a72ccc201f284573bed4a07945&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/576/851cb2.gif" width=70 border=0></A></textarea><br/><input type='button' value='提交' onclick='Replace()'/> 先获取图片地址,然后再重新拼接成<img src="图片地址"/>,这样应该更方便一些获取图片地址的正则:(src\s*=\s*)([\s\S]+?)(\""|\') <script>function Replace(){ var ta=document.getElementById("ta"); ta.value= ta.value.replace(/<a[^>]*?>(?:(?!<img|<\/a)[\s\S])*<img[^>]*?src=["']?([^"'>]*)["']?(?:(?!<\/a)[\s\S])*<\/a>/ig,"<img src='$1' />");}</script><body> <textarea style="width:400px;height:120px" id="ta"><A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FMAXBreast%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=681805886&t=1204983753&e=811982a72ccc201f284573bed4a07945&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/576/851cb2.gif" width=70 border=0></A></textarea><br/><input type='button' value='提交' onclick='Replace()'/> 加个\s 精细点...ta.value= ta.value.replace(/<a[^>]*?>(?:(?!<img|<\/a)[\s\S])*<img[^>]*?src=["']?([^"'>\s]*)["']?(?:(?!<\/a)[\s\S])*<\/a>/ig,"<img src='$1' />"); 问题在你把代码从这里拷贝出去后,要删除其中csdn给增加的空格,否则就不正确了。str = str.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)>(.*?)$/i,"<img src=\"$2\">")我再发看看还给加空格不。 为什么我发的系统会在两个<>括号两边给我多加空格呢,上面所有的都是,我就纳闷了。 回10楼,he_8134 你的代码如果不输入带超链接的内容,就失效了不能去除width="60" height="40"<script>function Replace(){ var ta=document.getElementById("ta"); ta.value= ta.value.replace(/<a[^>]*?>(?:(?!<img|<\/a)[\s\S])*<img[^>]*?src=["']?([^"'>]*)["']?(?:(?!<\/a)[\s\S])*<\/a>/ig,"<img src='$1' />");}</script><body> <textarea style="width:400px;height:120px" id="ta"><img src='http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/576/851cb2.gif' width="60" height="40" /></textarea><br/><input type='button' value='提交' onclick='Replace()'/> 回myvicy,我已经删除所有空格,还是失效请用插入源代码的方式,再贴一次看看? <script language="javascript"> str = '<A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FMAXBreast%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=865798593&t=1204978972&e=5375debee73a5972af40b3daa0e761d1&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/576/851cb2.gif" width=70 border=0></A>' //str = str.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)>(.*?)$/i," <img src=\"$2\"> ") str = str.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)>(.*?)$/i,"<img src=\"$2\">") alert(str); </script> 本地测试没有问题。 myvicy 我知道这样没问题,但是你写个表单试一试?就发现问题了。 下面是我测试过没有问题的代码,环境2003+ie 6.0<script>function Replace(){ var ta=document.getElementById("ta"); ta.value= ta.value.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)>(.*?)$/i,"<img src=\"$2\">");}</script><body> <textarea style="width:400px;height:120px" id="ta"><A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FMAXBreast%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=865798593&t=1204978972&e=5375debee73a5972af40b3daa0e761d1&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/576/851cb2.gif" width=70 border=0></A></textarea><br/><input type='button' value='提交' onclick='Replace()'/> 哈哈哈,非常感谢myvicy,你这次贴的代码没有问题了30分是你的了。马上结 等等,还是有个可怕的问题!插入多张图片后,最后只乘一张了<script>function Replace(){ var ta=document.getElementById("ta"); ta.value= ta.value.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)>(.*?)$/i,"<img src=\"$2\">");}</script><body> <textarea style="width:400px;height:120px" id="ta"><P><A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FFetal%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=1576142391&t=1205063728&e=955751a7f884268a5ab158f592a8a7c9&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/575/dfa4f9.gif" width=70 border=0></A><A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FFetal%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=1576142391&t=1205063728&e=955751a7f884268a5ab158f592a8a7c9&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/575/dfa4f9.gif" width=70 border=0></A><A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FFetal%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=1576142391&t=1205063728&e=955751a7f884268a5ab158f592a8a7c9&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/575/dfa4f9.gif" width=70 border=0></A><A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FFetal%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=1576142391&t=1205063728&e=955751a7f884268a5ab158f592a8a7c9&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/575/dfa4f9.gif" width=70 border=0></A><A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FFetal%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=1576142391&t=1205063728&e=955751a7f884268a5ab158f592a8a7c9&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/575/dfa4f9.gif" width=70 border=0></A></P></textarea><br/><input type='button' value='提交' onclick='Replace()'/> 图片链接代码:http://www.fluorescencestar.com/imge/LED%20cbdxz.gif js 给html中得class添加样式 在ie6 7下不显示图片,代码如下 .net开发的类似桌面系统的代码 javascript [array]題目 请大家帮我检查一个控制层的显示与隐藏的问题 为什么在ie下,上下左右没用呢? JS验证问题,求高手指点,在线等待...急 梅花雪,非常感谢! Extjs如何一键展开tree,数据量有几千行 请帮帮我呀 请教:如何改变span标记间的文本?急! js如何获取dom的父节点类型?急,多谢! js的 exec和 execCommand 有什么区别?
<script language="javascript">
str = '<A href="http://rd.union.qihoo.com/ target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/kkk.gif" width=70 border=0 onload="javascript:if(this.width>120)this.width=120;if(this.height>120)this.height=120;"></A>'
str = str.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)>(.*?)$/i,"<img src=\"$2\">")
alert(str);
</script>
如果内容变成这样,就失效了
<A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FMAXBreast%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=865798593&t=1204978972&e=5375debee73a5972af40b3daa0e761d1&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/576/851cb2.gif" width=70 border=0></A>不能替换成这样的干净代码
<IMG src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/576/851cb2.gif">
str = '<A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FMAXBreast%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=865798593&t=1204978972&e=5375debee73a5972af40b3daa0e761d1&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/576/851cb2.gif" width=70 border=0></A>'
//str = str.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)>(.*?)$/i," <img src=\"$2\"> ")
str = str.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)>(.*?)$/i," <img src=\"$2\"> ")
alert(str);
</script>
但这样加个表单就不行了!
帮忙看一下!
<script>
function Replace()
{
var ta=document.getElementById("ta");
ta.value= ta.value.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)> (.*?)$/i," <img src=\"$2\">")
}
</script>
<body>
<textarea style="width:400px;height:120px" id="ta">
<A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FMAXBreast%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=681805886&t=1204983753&e=811982a72ccc201f284573bed4a07945&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/576/851cb2.gif" width=70 border=0></A>
</textarea><br/>
<input type='button' value='提交' onclick='Replace()'/>
function Replace()
{
var ta=document.getElementById("ta");
ta.value= ta.value.replace(/<a[^>]*?>(?:(?!<img|<\/a)[\s\S])*<img[^>]*?src=["']?([^"'>]*)["']?(?:(?!<\/a)[\s\S])*<\/a>/ig,"<img src='$1' />");
}
</script>
<body>
<textarea style="width:400px;height:120px" id="ta">
<A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FMAXBreast%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=681805886&t=1204983753&e=811982a72ccc201f284573bed4a07945&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/576/851cb2.gif" width=70 border=0></A>
</textarea><br/>
<input type='button' value='提交' onclick='Replace()'/>
ta.value= ta.value.replace(/<a[^>]*?>(?:(?!<img|<\/a)[\s\S])*<img[^>]*?src=["']?([^"'>\s]*)["']?(?:(?!<\/a)[\s\S])*<\/a>/ig,"<img src='$1' />");
str = str.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)>(.*?)$/i,"<img src=\"$2\">")
我再发看看还给加空格不。
你的代码如果不输入带超链接的内容,就失效了
不能去除width="60" height="40"
<script>
function Replace()
{
var ta=document.getElementById("ta");
ta.value= ta.value.replace(/<a[^>]*?>(?:(?!<img|<\/a)[\s\S])*<img[^>]*?src=["']?([^"'>]*)["']?(?:(?!<\/a)[\s\S])*<\/a>/ig,"<img src='$1' />");
}
</script>
<body>
<textarea style="width:400px;height:120px" id="ta">
<img src='http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/576/851cb2.gif' width="60" height="40" /></textarea><br/>
<input type='button' value='提交' onclick='Replace()'/>
请用插入源代码的方式,再贴一次看看?
<script language="javascript">
str = '<A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FMAXBreast%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=865798593&t=1204978972&e=5375debee73a5972af40b3daa0e761d1&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/576/851cb2.gif" width=70 border=0></A>'
//str = str.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)>(.*?)$/i," <img src=\"$2\"> ")
str = str.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)>(.*?)$/i,"<img src=\"$2\">")
alert(str);
</script> 本地测试没有问题。
<script>
function Replace()
{
var ta=document.getElementById("ta");
ta.value= ta.value.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)>(.*?)$/i,"<img src=\"$2\">");
}
</script>
<body>
<textarea style="width:400px;height:120px" id="ta"><A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FMAXBreast%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=865798593&t=1204978972&e=5375debee73a5972af40b3daa0e761d1&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/576/851cb2.gif" width=70 border=0></A></textarea><br/>
<input type='button' value='提交' onclick='Replace()'/>
30分是你的了。马上结
<script>
function Replace()
{
var ta=document.getElementById("ta");
ta.value= ta.value.replace(/.*?<img(.*?)src=\"(.*?)\"(.*?)>(.*?)$/i,"<img src=\"$2\">");
}
</script>
<body>
<textarea style="width:400px;height:120px" id="ta"><P><A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FFetal%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=1576142391&t=1205063728&e=955751a7f884268a5ab158f592a8a7c9&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/575/dfa4f9.gif" width=70 border=0></A><A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FFetal%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=1576142391&t=1205063728&e=955751a7f884268a5ab158f592a8a7c9&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/575/dfa4f9.gif" width=70 border=0></A><A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FFetal%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=1576142391&t=1205063728&e=955751a7f884268a5ab158f592a8a7c9&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/575/dfa4f9.gif" width=70 border=0></A><A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FFetal%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=1576142391&t=1205063728&e=955751a7f884268a5ab158f592a8a7c9&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/575/dfa4f9.gif" width=70 border=0></A><A href="http://rd.union.qihoo.com/stat.html?go=http%3A%2F%2Fwww.100bj.cn%2Fzt%2FFetal%2Findex.aspx&m=183379&p=2001&a=2039&b=1003&oid=1576142391&t=1205063728&e=955751a7f884268a5ab158f592a8a7c9&r=http%3A%2F%2F192.168.1.5%2F2008bt%2FTopicShow.asp%3FThreadID%3D5627&refid=" target=_blank><IMG height=50 alt="" src="http://img.qihoo.com/qhimg/bbs_img/70_50/1/419/575/dfa4f9.gif" width=70 border=0></A></P></textarea><br/>
<input type='button' value='提交' onclick='Replace()'/>
http://www.fluorescencestar.com/imge/LED%20cbdxz.gif