1

I'm using this line to change a specific word in a text to other word:

 document.body.innerHTML = document.body.innerHTML.replace(/katt/g, "smurf");

Now I'm trying to use the same line to change an image URL in HTML but it doesn't work. I trying this code:

document.body.innerHTML = document.body.innerHTML.replace(/"https://oldimage.jpg"/g, "https://newimage.jpg");

1 Answer 1

1

Escape // inside Regex like \/\/. You also should not have surrounding "":

document.body.innerHTML = "https://oldimage.jpg"
document.body.innerHTML = document.body.innerHTML.replace(/https:\/\/oldimage.jpg/g, "https://newimage.jpg");

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.