日期:2014-05-16  浏览次数:20573 次

又一正则表达式问题,求高手相助
如下,把[原字符串]替换变成[目标结果]

====   原字符串   ========================================================
....
<?xml:namespace   prefix   =   o   ns   =   "urn:schemas-microsoft-com:office:office "   /> <o:lock   aspectratio= "t "   v:ext= "edit "> </o:lock> </v:shapetype> <v:shape   id=_x0000_i1025   style= "WIDTH:   464.25pt;   HEIGHT:   55.5pt "   type   =   "#_x0000_t75 "   coordsize   =   "21600,21600 "> <v:imagedata   o:title= "aa "   src   =   "file:///C:\DOCUME~1\陈金发\LOCALS~1\Temp\msohtml1\01\clip_image001.png "> </v:imagedata> </v:shape>
...

====   目标结果   ========================================================
....
<img   src   =   "file:///C:\DOCUME~1\陈金发\LOCALS~1\Temp\msohtml1\01\clip_image001.png "   />
...


怎么写?



------解决方案--------------------
<script language=javascript>
var str= ' <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office " /> <o:lock aspectratio= "t " v:ext= "edit "> </o:lock> </v:shapetype> <v:shape id=_x0000_i1025 style= "WIDTH: 464.25pt; HEIGHT: 55.5pt " type = "#_x0000_t75 " coordsize = "21600,21600 "> <v:imagedata o:title= "aa " src = "file:///C:\DOCUME~1\陈金发\LOCALS~1\Temp\msohtml1\01\clip_image001.png "> </v:imagedata> </v:shape> '
re=/(src\s*=\s*\ "[^ "]+\ ")/i
re.test(str)
str= " <img "+RegExp.$1+ "/> "
alert(str)
</script>
------解决方案--------------------
<script >
str= ' <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office " /> <o:lock aspectratio= "t " v:ext= "edit "> </o:lock> </v:shapetype> <v:shape id=_x0000_i1025 style= "WIDTH: 464.25pt; HEIGHT: 55.5pt " type = "#_x0000_t75 " coordsize = "21600,21600 "> <v:imagedata o:title= "aa " src = "file:///C:\DOCUME~1\陈金发\LOCALS~1\Temp\msohtml1\01\clip_image001.png "> </v:imagedata> </v:shape> '
var re=/(src \= \ ".[^\ "]+\ ")/gi;
re.test(str);
str= ' <img '+RegExp.$1+ '> ';
alert(str);
</script>

------解决方案--------------------
前后不为空,从什么地方开始要保留?下面是只把 <v:imagedata 替换掉的正则
<script language=javascript>
var str= ' <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office " /> <o:lock aspectratio= "t " v:ext= "edit "> </o:lock> </v:shapetype> <v:shape id=_x0000_i1025 style= "WIDTH: 464.25pt; HEIGHT: 55.5pt " type = "#_x0000_t75 " coordsize = "21600,21600 "> <v:imagedata o:title= "aa " src = "file:///C:\DOCUME~1\陈金发\LOCALS~1\Temp\msohtml1\01\clip_image001.png "> </v:imagedata> </v:shape> '
re=/ <v:imagedata(.+?)(src\s*=\s*\ "[^ "]+\ ")(.+?) <\/v:imagedata> /i
re.test(str)
str=str.replace(re, " <img "+RegExp.$2+ "/> ")
alert(str)
</script>

------解决方案--------------------
<script language=javascript>