【问题标题】:Replace value of id in URL替换 URL 中 id 的值
【发布时间】:2016-09-04 16:54:36
【问题描述】:

我有这种形式的网址:

https://www.merkel.com/obama?trump=15&id=9616071454&hilarry=es

例如,我想通过 将 9616071454 替换为 1。

我知道replace(),但这将替换“id”本身,不是“id”的值。

网络开发世界有什么共同点吗? :)

【问题讨论】:

  • str.replace(/\bid=\d+/, 'id=1')
  • 我会检查@AvinashRaj,但是can you please explain a bit? :)
  • How to Ask而言,您应该比这更了解。
  • @Amit 我唯一的尝试是使用replace(),你的意思是把它包括在我的问题中? Avinash,它有效,谢谢!谢谢提图斯!
  • @gsamaras \b 匹配单词 char 和非单词 char 或(反之亦然)。 \d+ 匹配一个或多个数字字符。所以上面的正则表达式将匹配整个 id 参数及其值。

标签: javascript javascript html string function url


【解决方案1】:

考虑以下情况的解决方案:

  • id参数可以包含除数字以外的其他字符
  • 避免在id 后跟# 时替换片段#

var str = 'https://www.foo.com/bar?trump=15&hilarry=es&id=961607some1454text#fragment',
    newId = 1,
    replaced = str.replace(/\bid=[^&#]+/g, "id=" + newId);

console.log(replaced); // "https://www.foo.com/bar?trump=15&hilarry=es&id=1#fragment"

【讨论】:

    【解决方案2】:

    只需硬编码 &id= 即可重新替换。

    var str = 'https://www.merkel.com/obama?trump=15&id=9616071454&hilarry=es';
    var str2 = 'https://www.merkel.com/obama?id=9616071454&trump=15&hilarry=es';
    var newId = '123';
    
    str = str.replace(/([&?])id=[0-9]+/, '$1id=' + newId);
    str2 = str2.replace(/([&?])id=[0-9]+/, '$1id=' + newId);
    
    alert(str);
    alert(str2);

    【讨论】:

    • 如果一开始就存在id怎么办? \b 将涵盖所有情况。
    【解决方案3】:

    它的简单模式匹配。模式匹配可以参考这个URL

    function(newValue,url) {
       url=url.replace(/id=\d+/,'id='+newValue);
       return url;
    }
    

    【讨论】:

    • 不错的尝试。确保您检查我的问题下的最高评论! ;) +1
    【解决方案4】:

    此功能有效,它允许您选择所需的方式参数。

    var exampleStrng ='trump=15&id=9616071454&hilarry=es'; // this is an example query string.
       var urlQry = window.document.location.search.substring(1); // you can use this in live code to get the query string.
    
    // reusable function for split in text.
    function strSpliter( str, splitVal ){
       return str.split(splitVal);
    }
    
     // function to reassign query parameter values.
    function changQry(qry, setParam, chngVal){
    var pnt = strSpliter(qry, '&'),//use the spliter function to change the query into an array split on the '&' character.
    newQryArr = [],  // a temp array to hold the new parameters and their value.
    newQry = '',// this will be the string where the query parameters and values are reconstructed into a string.
    newQryStr = '';// this will be the query with the new value.
    
    pnt.forEach( function( item, idx ){
    var param = strSpliter(item, '=');  //use the spliter function to split the parameter and their value.
    
     // checks the parameter against the one you want to change.
    if( param[0] === setParam ){
       param[1] = chngVal;// assigns the new value to the parameter. 
    
       newQryArr.push(param.join('=')); // rejoins the parameter and its value and pushes it into the temp array.
    } else {
       newQryArr.push(param.join('='));// rejoins the parameter and its value and pushes it into the temp array.
    }
    
    newQry = newQryArr.join('&');// rejoins all the parameters and their values.
    newQryStr = '?' + newQry;// creates the new query string.
    
    });
     return newQryStr; // returns the new search query string.
    
    }
    
      changQry(exampleStrng, 'id', 77777745);
    

    没有 cmets

    var urlQry = window.document.location.search.substring(1);

    function strSpliter( str, splitVal ){
       return str.split(splitVal);
    }
    
    function changQry(qry, setParam, chngVal){
        var pnt = strSpliter(qry, '&'),
               newQryArr = [],
               newQry = '',
               newQryStr = '';
    
    pnt.forEach( function( item, idx ){
       var param = strSpliter(item, '=');
    
       if( param[0] === setParam ){
          param[1] = chngVal;
          newQryArr.push(param.join('='));
       } else {
          newQryArr.push(param.join('='));
       }
    
       newQry = newQryArr.join('&');
       newQryStr = '?' + newQry;
    });
       return newQryStr;
    }
    
      changQry(urlQry, 'id', 77777745);
    

    【讨论】:

    • 太密集了...也许我有时间会阅读它,安德烈,谢谢! :)
    猜你喜欢
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 2017-03-04
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 2014-11-19
    相关资源
    最近更新 更多