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

又一正则表达式有关问题,求高手相助

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

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

====   原字符串   ========================================================
....
<?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>


var str= 'asdasd <?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> fghfgh '
re=/ <\?xml:(.+?)(src\s*=\s*\ "[^ "]+\ ")(.+?) <\/v:shape> /i
str=str.replace(re, " <img $2/> ")
alert(str)
</script>
[解决办法]
加个g就可以了
re=/ <\?xml:(.+?)(src\s*=\s*\ "[^ "]+\ ")(.+?) <\/v:shape> /ig

热点排行