gsamaras gsamaras - 5 months ago 29
HTML Question

Replace value of id in URL

I have URLs of that form:

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? :)


The solution considering situations when:

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

var str = '',
    newId = 1,
    replaced = str.replace(/\bid=[^&#]+/g, "id=" + newId);

console.log(replaced); // ""