gsamaras gsamaras - 1 year ago 87
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? :)

Answer Source

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); // ""
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download