gsamaras
gsamaras

Reputation: 73366

Replace value of id in URL

I have URLs of that form:

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

I would like, via to replace 9616071454 with 1, for example.

I know about the replace(), but this will replace "id" itself, not the value of "id".

Is there anything common in the web dev world? :)

Upvotes: 1

Views: 4447

Answers (4)

andre mcgruder
andre mcgruder

Reputation: 1520

This function works and it allows you to pick way parameter you want.

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

without comments

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

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

The solution considering situations when:

  • id param can contain other characters besides digits
  • avoiding fragment # replacement when id is followed by #

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"

Upvotes: 2

Madie
Madie

Reputation: 241

Its simple pattern matching. You can refer to this URL about pattern matching.

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

Upvotes: 1

Justinas
Justinas

Reputation: 43471

Simply hard-code that &id= to be re-replaced.

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

Upvotes: 2

Related Questions