首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

倾家10分,求一正则!该如何解决

2012-03-19 
倾家10分,求一正则!想用编辑框webhtmleditor把数据写到数据库的时候替换一些img标签:把所有的img标签替换

倾家10分,求一正则!
想用编辑框webhtmleditor把数据写到数据库的时候替换一些img标签:
把所有的img标签替换成下面的形式:
  <img   src= "http://www.caiyuantong.com/eWebEditor_v280_Free/UploadFile/2007713132227970.jpg "   onclick= "javascript:window.open(this.src); "   style= "cursor:   pointer; "   onmousewheel= "return   bbimg(this) "   onload= "javascript:if(this.width> screen.width-500)this.style.width=screen.width-500; "   border= "0 ">

原来的img标签可能是 <img....../> ,也可能是 <img.......> ,也可能是 <img.....> </img>
非常感谢!
恳请帮忙
是C#

[解决办法]
过客老大你好,我发的帖子:http://community.csdn.net/Expert/topic/5653/5653616.xml?temp=.8262903
倾家10分,求一正则!
多谢您的帮助,问题得到解决,但是我认为其中还是存在着一个小问题:
比如原来的源代码是: <img src= "/yinuof/UserFolder/284a70f04ae66fafa50f525a.jpg " onclick= "javascript:window.open(this.src); " style= "cursor: pointer; " onload= "javascript:if(this.width> screen.width-500)this.style.width=screen.width-500; " border= "0 " width= "678 ">

如果用Regex.Replace(allText, @ " <img[^> ]*src=([ ' " "]?)(? <content> [^ ' " "\s> ]*)\1[^> ]*> ( </img> )? ", @ " <img src= " "${content} " " onclick= " "javascript:window.open(this.src); " " style= " "cursor:pointer; " " onload= " "javascript:if(this.width> screen.width-500)this.style.width=screen.width-500; " " border= " "0 " "> ", RegexOptions.IgnoreCase);
进行替换,他会变成:
<img .....> screen.width-500)this.style.width=screen.width-500; " border= "0 "&gt;

后面的那个screen.width-500)this.style.width=screen.width-500; " border= "0 "&gt;是多加的啊
-------------------------------------------------------
对于你站内信提到的这种情况,因为你的 <img...> 中出现了> 这个符号造成的,需要修改一下原来的正则
因为上面同样出现了style= "cursor: pointer; " 这种情况,也就是 " "内有空格的情况,所以类似于src=/abc/xyz.jpg这种=后直接接网址的情况也就不再考虑,这里只考虑了=后接 "的情况
修改后的方法如下

string yourStr = .............;
string result = Regex.Replace(yourStr, @ " <img(?=.*?src= " "(? <content> [^ " "]*) " ")\s+(?:\w+= " "[^ " "]* " "\s*)*[^> ]*> (?: </img> )? ", @ " <img src= " "${content} " " onclick= " "javascript:window.open(this.src); " " style= " "cursor: pointer; " " onmousewheel= " "return bbimg(this) " " onload= " "javascript:if(this.width> screen.width-500)this.style.width=screen.width-500; " " border= " "0 " "> ", RegexOptions.IgnoreCase);


如果把=后接 '的情况也考虑在内,这样改下
string yourStr = ................;
string result = Regex.Replace(yourStr, @ " <img(?=.*?src=([ ' " "])(? <content> [^ " "]*)\1)\s+(?:\w+= " "[^ " "]* " "\s*)*[^> ]*> (?: </img> )? ", @ " <img src= " "${content} " " onclick= " "javascript:window.open(this.src); " " style= " "cursor: pointer; " " onmousewheel= " "return bbimg(this) " " onload= " "javascript:if(this.width> screen.width-500)this.style.width=screen.width-500; " " border= " "0 " "> ", RegexOptions.IgnoreCase);

热点排行