ohval
ohval

Reputation: 15

Replace part of string with other text?

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/

Upvotes: 0

Views: 563

Answers (2)

Prakash Harvani
Prakash Harvani

Reputation: 1041

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.

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522636

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');

Upvotes: 1

Related Questions