0

I have a variable

link = 'dog.jpg'

How do I write code to change link to = dog.webm instead?

I've tried link.text.replace('jpg', 'webm'); but it has no effect?

https://jsfiddle.net/m5Lp6a1z/

ohval
  • 15
  • 4
  • 1
    `link = link.replace(...)` – Barmar Aug 14 '20 at 05:54
  • Use `link.replace('jpg', 'webm')` no need of .text – keidakida Aug 14 '20 at 05:54
  • Does this answer your question? [How can I perform a str\_replace in JavaScript, replacing text in JavaScript?](https://stackoverflow.com/questions/5519368/how-can-i-perform-a-str-replace-in-javascript-replacing-text-in-javascript) – cstayyab Aug 14 '20 at 06:02

2 Answers2

1

You need to make an actual assignment back to the link variable:

link = link.replace('jpg', 'webm');

But actually, a regex replacement targeting only JPEG extensions would be probably be safer here:

link = link.replace(/\.jpg$/, '.webm');
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

If you want to Replace Text Then you need to try this,

 let link = 'dog.jpg'

 link.replace('jpg', 'webm')

you don't need to write link.text.replace

It will help you.

Prakash Harvani
  • 1,007
  • 7
  • 18