OBAID OBAID - 4 months ago 6
Javascript Question

.replace javascript not working

Hi i am working in java script i have a string

var data = 'http://baab.bh/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/t/e/test.jpg';


i want to replace /image/ into 'image/440x600' i am using this function


.replace()


but its not working here is my code

var data ='http://baab.bh/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/t/e/test.jpg';
data.replace('/image/', '/image/440x600/');
console.log(data);


its showing same not replacing /image/ into 'image/440x600'.

Answer

Strings in JavaScript are immutable. They cannot be modified.

The replace method returns the modified string, it doesn't modify the original in place.

You need to capture its return value.

var data = 'http://baab.bh/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/t/e/test.jpg';
data = data.replace('/image/', '/image/440x600/');
console.log(data);