1

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;
2
  • 4
    That seems like a bad way of doing it... You're destroying the entire DOM and rebuilding it, all to change... what, a link's URL? You'd be better off selecting the link and changing its .href Commented Nov 24, 2019 at 20:32
  • You might need to escape the last / in your regex, such as /www.sitoweb.it\//gi. But above advice is a far better idea Commented Nov 24, 2019 at 20:35

2 Answers 2

1

escape / in regular expression:

/www.sitoweb.it\//gi
Sign up to request clarification or add additional context in comments.

3 Comments

Why? AFAIK forward slashes have no special meaning in regular expressions, except in Perl, where they may delimit the regex. But this isn't a Perl question.
in javascript they have no meaning in The Regular Expression but it is a key charecter and its indicates start point and end point of a regular expr in a string. you noticed that in the example above after indication the end of regular expression with "/" i use "g" for global and "i" for in-case-sensitive those are not as part of The regular Expression.
Cool, thanks for clarifying. Maybe edit this into your answer?
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.