【问题标题】:Javascript Reg Expression to replace Get Parameter in the URLJavascript 正则表达式替换 URL 中的获取参数
【发布时间】:2013-06-12 08:59:04
【问题描述】:

使用正则表达式,我想编写一个函数,该函数将采用 URL 和参数名称:ReplaceParamValueinURL (url, param, value)

如果参数存在,它将替换URL中的值。
如果参数不存在,它会将其与值一起添加到 URL。
如果参数存在但没有值,它会将值添加到参数中。

在正则表达式查找和替换中是否有一种优雅的方式来完成这三个操作?

ReplaceParamValueinURL ("http://google.com?a=1&b=2&c=3, a , 4)
returns http://google.com?a=4&b=2&c=3 

ReplaceParamValueinURL ("http://google.com?a=1&b=2&c=3, a , 4)
returns http://google.com?a=4&b=2&c=3 

ReplaceParamValueinURL ("http://google.com?a=1&b=2&c=3, c , 4)
returns http://google.com?a=1&b=2&c=4 

ReplaceParamValueinURL ("http://google.com?a=1&b=2&c=3, d , 5)
returns http://google.com?a=1&b=2&c=3&d=5 

ReplaceParamValueinURL ("http://google.com?aaa=0&a=1&b=2&c=3, a , 6)
returns http://google.com?aaa=0&a=6&b=2&c=3 


ReplaceParamValueinURL ("http://google.com?a=1&b&c=3, b , 2)
returns http://google.com?a=1&b=2&c=3 

I am hoping to do this with Reg ex instead of split. I really appreciate it if you can explain your answer if the regex is too complex. Is there a jQuery function that already does this? 

我想这是一个很常见的情况,但可能有很多极端情况。

ReplaceParamValueinURL ("http://google.com?a=1&b&c=3#test, a , 2)
returns http://google.com?a=2&b&c=3#test

【问题讨论】:

    标签: javascript jquery regex


    【解决方案1】:

    不,你不能用一个正则表达式来做,但是这个函数很简单,我已经用你所有的例子测试了它,所以它应该可以工作:

    function ReplaceParamValueinURL (url, name, val) {
    
        //Try to replace the parameter if it's present in the url
        var count = 0;
        url = url.replace(new RegExp("([\\?&]" + name + "=)[^&]+"), function (a, match) {
            count = 1;
            return match + val;
        });
    
        //If The parameter is not present in the url append it
        if (!count) {
            url += (url.indexOf("?") >=0 ? "&" : "?") + name + "=" + val;
        }
    
        return url;
    }
    

    【讨论】:

    • 嗨,我刚刚意识到这个解决方案不适用于我的真实网址localhost:8080/…
    【解决方案2】:

    试试这个,

    function ReplaceParamValueinURL(url , replceparam , replaceValue)
     {
      regExpression = "(\\?|&)"+replceparam+"(=).(&|)";
      var regExpS = new RegExp(regExpression, "gm");
      var getmatch = url.match(regExpS);
      var regExpSEq = new RegExp("=", "g");
      var getEqalpostion = regExpSEq.exec(getmatch);
      var newValue;
      if(getmatch[0].charAt(getmatch[0].length - 1) != "&")
        {
          var subSrtingToReplace  = getmatch[0].substring((getEqalpostion.index+ 1),getmatch[0].length );
          newValue =  getmatch[0].replace(subSrtingToReplace , replaceValue);
        }
      else
       {
          var subSrtingToReplace  = getmatch[0].substring((getEqalpostion.index+ 1) , getmatch[0].length - 1 );
          newValue =  getmatch[0].replace(subSrtingToReplace , replaceValue);
       }
    
     return  returnString = url.replace(regExpS , newValue);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-03
      • 2014-08-21
      • 2019-01-27
      • 1970-01-01
      • 2013-01-22
      • 2010-11-12
      • 2016-09-23
      • 1970-01-01
      相关资源
      最近更新 更多