再问一个正则表达式
var msgstr= 'aaaa <img src= "http://127.0.0.1/sms/faces/01.gif " width= "19 " height= "19 "> bbbb <img src= "http://127.0.0.1/sms/faces/02.gif " width= "19 " height= "19 "> 'cc;
现在我想把上面的替换成
aaaa[/:FC01]bbbb[/:FC02]cc
但是,必须路径在faces文件夹下的图片才替换成[/:FC01]这种形式,否则不替换
------解决方案--------------------━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
※以下是引用楼主 zhaolili8881() 在2007-07-20 14:12:38 的发言:
──────────────────────────────────────────
以上答案都有点问题,
如果把 <img src= "http://127.0.0.1/sms/faces/01.gif " width= "19 " height= "19 ">
改成
<img src= "http://127.0.0.3/sms/faceddds/01.gif " width= "19 " height= "19 ">
结果就不对了,
还有就是http://127.0.0.1/sms/这部分路径是不定的,但faces/01.gif这部分是定的
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var msgstr= 'aaaa <img src= "http://127.0.0.1/sms/faces/01.gif " width= "19 " height= "19 "> bbbb <img src= "http://127.0.0.1/sms/faces/02.gif " width= "19 " height= "19 "> cc ';
alert(msgstr.replace(/ <img(?:\s+[^\s> ]+)*\s+src= ".*?faces\/([^.]+).gif "(?:\s+[^\s> ]+)*> /ig, "[/:FC$1] "));
------解决方案-------------------- <script>
var str= 'aaaa <img src= "http://127.0.0.1/sms/facddddddddes/01.gif " width= "19 " height= "19 "> bbbb <img src= "http://127.0.0.1/sms/faces/02.gif " width= "19 " height= "19 "> cc ';
var re=/ <img\ssrc=\ "[^\ "]*?faces\/(\d{2})\.gif\ "[\s\S]*?> /g;
str=str.replace(re, "[/:FC$1] ");
alert(str);
</script>