读书人

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

发布时间: 2012-03-03 15:33:04 作者: rapoo

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

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

读书人网 >JavaScript

热点推荐