后台获取得字符串:
<p><img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /> 1111 <img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /></p>要转换成<p><img style="float:none;margin:0px;" alt="" src="htt://feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /> 1111 <img style="float:none;margin:0px;" alt="" src="htt://feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /></p>要找出原字符串中的所有img标签的src属性例如:feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png,拼接成http://feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png替换原来的src属性!难!!!

解决方案 »

  1.   

    http://feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png这个url 也不对啊               <script>
                   var s='<p><img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /> 1111 <img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /></p>';
                   alert(s.replace(/<img (.*?)src="(.+?)"(.*?)>/,'<img $1src="http://$2"$3>'));
                   </script>
      

  2.   

    这个url是不对的 只是举个例子的 
    你写的那段js不能运行啊 
      

  3.   


    var s='';
    s= s.replace(/src="([^"]+)"/gi, 'src="http://$1"'); 
      

  4.   

    <html>
    <head>
    <script>
    function rp(){
    var h = document.getElementById("test").innerHTML;
    alert(h.replace(/<img(.*?)src="[^http:\/\/](.+?)"(.*?)>/g,'<img$1src="http://$2"$3>'));}
    </script></head>
    <body>
    <div id="test">
    <p><img style="float:none;margin:0px;" alt="" src="http://feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /> 1111 <img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /></p>
    </div>
    <input type="button" value="替换" onclick="rp()"/>
    </body>
    </html>
      

  5.   


      <html>
    <head>
        <title>无标题页</title>
        <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                var s = '<p><img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /> 1111 <img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /></p>';
                $("#btn").click(function() {
                    var patt = new RegExp(/<img[\s|\S|\s]*?src=[\s|\"|\']*([^>\"\']*)[\s|\S]*?>/ig);
                    while ((result = patt.exec(s)) != null) {
                        s = s.replace(result[1], "http://"+result[1]);
                    }
                    alert(s);
                });
            });
        </script>
    </head>
    <body>
      <input type="button" id="btn" onclick="test()" value="测试" />
    </body>
    </html>
      

  6.   


    刚弄错了  这个是对的 你可以测试下 直接复制 运行<html>
    <head>
    <script>
    function gg(){
    var s="<img src='feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png' />";
    s= s.replace(/src=(')/gi, 'src=$1http://'); 
    alert(s);
    }
    </script>
    </head>
    <body>
    <a href="#" onclick="gg();">替换</a>
    </body>
    </html>
      

  7.   

    再发一次 <html>
    <head>
    <script>
    function gg(){
    var s="<img src='feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png' />";
    s= s.replace(/src=(')/g, 'src=$1http://'); 
    alert(s);
    }
    </script>
    </head>
    <body>
    <a href="#" onclick="gg();">替换</a>
    </body>
    </html>
      

  8.   

    发错了 上面的代码  这个才OK的
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>无标题页</title>
        <script type="text/javascript">
            function test() {
                var s = '<p><img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /> 1111 <img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /></p>';
                var patt = new RegExp(/<img[\s|\S|\s]*?src=[\s|\"|\']*([^>\"\']*)[\s|\S]*?>/ig);
                while ((result = patt.exec(s)) != null) {
                    s = s.replace(result[1], "http://" + result[1]);
                }
                alert(s);
            }
        </script>
    </head>
    <body>
      <input type="text" id="txtVal" />&nbsp;<input type="button" id="btn" onclick="test()" value="测试" />
    </body>
    </html>
      

  9.   

    我那个没问题 src 如果用 单引号括起来 就用 s= s.replace(/src=(')/g, 'src=$1http://'); 如果是双引号括起来 就用s= s.replace(/src=(")/g, 'src=$1http://'); 
      

  10.   

      <script>
      var s='<p><img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /> 1111 <img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /></p>';
      alert(s.replace(/<img (.*?)src="(.+?)"(.*?)>/ig,'<img $1src="http://$2"$3>'));
      </script> 之前随手写的,没考虑大小写和多匹配g (全文查找出现的所有 pattern)
    i (忽略大小写)
    m (多行查找)