Roberto Paolucci
Roberto Paolucci

Reputation: 11

String Replace in a URL

I need to replace the URL in the DIV with ID "___gcse_1". I created a new function, replace_url (), but adding the domain in full does not work, as there are syntax problems. my code below:

async function sostituisco_url() {

  var str = document.getElementById("___gcse_1").innerHTML;
  var res = str.replace(/www.sitoweb.it//gi, "nuova_url");

  document.getElementById("___gcse_1").innerHTML = res;
}

window.onload = sostituisco_url;

Upvotes: 1

Views: 205

Answers (2)

Abilogos
Abilogos

Reputation: 5010

escape / in regular expression:

/www.sitoweb.it\//gi

Upvotes: 1

TopW3
TopW3

Reputation: 1527

Please try this. str.replace(/www.sitoweb.it//gi, "nuova_url");

Upvotes: 0

Related Questions