有字符串如下:
手机的饭卡手机的疯狂拉升金光华附近撒旦回复 
http://www.xxx.com/骄傲深刻理解的疯狂升级啊对抗疗法。其中url地址可能是如下三种情况:
http://www.xxx.com/http://www.xxx.com/mall/default.aspx
http://www.xxx.com/mall/default.aspx?id=2现在想用js配合正则,将已有参数加入到字符串中,参数是re=100,
所以匹配之后的字符串中url地址部分可能是如下三种情况:
http://www.xxx.com/?re=100
http://www.xxx.com/mall/default.aspx?re=100
http://www.xxx.com/mall/default.aspx?id=2&re=100求大侠给个配合正则替换的js方法js正则

解决方案 »

  1.   

    function ax(a){
    var begin=a.indexOf("?");
    if(begin==-1){
    a+="?re=100";
    }else{
    a+="&re=100";
    }
    return a;
    }
    var arr=['http://www.xxx.com/','http://www.xxx.com/mall/default.aspx','http://www.xxx.com/mall/default.aspx?id=2'];
    for(var i in arr){
    alert(ax(arr[i]));
    }
    ??
      

  2.   

    似梦飞花 不是这样的 字符串中间包含一个url地址,如果用你的方法 会在字符串末尾加上re=100 
      

  3.   

    <script type="text/javascript">
    var t1 = '手机的饭卡手机的疯狂拉升金光华附近撒旦回复 http://www.xxx.com/骄傲深刻理解的疯狂升级啊对抗疗法。';
    var t2 = '手机的饭卡手机的疯狂拉升金光华附近撒旦回复 http://www.xxx.com/mall/default.aspx骄傲深刻理解的疯狂升级啊对抗疗法。';
    var t3 = '手机的饭卡手机的疯狂拉升金光华附近撒旦回复 http://www.xxx.com/mall/default.aspx?id=2骄傲深刻理解的疯狂升级啊对抗疗法。';
    var t4 = '手机的饭卡手机的疯狂拉升金光华附近撒旦回复 http://www.xxx.com/mall/mall32132/default.aspx?id=2骄傲深刻理解的疯狂升级啊对抗疗法。';
    var add = 're=100';
    String.prototype.addParam = function(s){
    var re = /http:\/\/([^\/]+\/)+([^.]+\.aspx(\?[^=]+=\d+)?)?/gi,
    t = this;
    return t.replace(re, function($){
    return $ + ['?', '&'][$.indexOf('?') == -1 ?  0 : 1 ] + s;
    })
    };
    alert( t1.addParam(add) );
    alert( t2.addParam(add) );
    alert( t3.addParam(add) );
    alert( t4.addParam(add) );

    </script>