替换URL中的id值

时间:2016-09-04 16:54:37

标签: javascript html string function url

我有这种形式的网址:

  

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

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

我知道replace(),但这会取代“id”本身,不是“id”的值。

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

4 个答案:

答案 0 :(得分:2)

只需硬编码java.sql.Driver即可重新替换。



com.mysql.jdbc.Driver




答案 1 :(得分:2)

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

  • # param可以包含除数字之外的其他字符
  • 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"
$(".submit").on("click", function(e) {
  e.preventDefault();
  var first=$("#first").val();
  var last=$("#last").val();
  var email=$("#email").val();
  var comments=$("#comments").val();
  var txt1="Welcome " +first+" "+last;
  var txt2="Your Email is " +email;
  var txt3="Your Message is "+ comments;
  $("#targetDiv").append(txt1+"<br>"+txt2+"<br>"+txt3);
});

答案 2 :(得分:1)

简单的模式匹配。您可以参考此URL关于模式匹配。

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

答案 3 :(得分:0)

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

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);

没有评论

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);