HalesEnchanted HalesEnchanted - 5 months ago 10
Javascript Question

Remove characters from image url retrieved as string?

How do I remove the image resolution from the string below in Node.js?


http://asset.beyonce.com/wp-content/uploads/2016/06/XIII6614-800x800.jpg


I'd like it to be just


http://asset.beyonce.com/wp-content/uploads/2016/06/XIII6614.jpg
without the -800x800!


var image = 'http://asset.beyonce.com/wp-content/uploads/2016/06/XIII6614-800x800.jpg';

image = image.replace(new RegExp("^(.*?)-\d+x\d+\.([^/]+)$", "g"), "")
console.log(image);


The above code isn't working for some reason?

Answer
var image = 'http://asset.beyonce.com/wp-content/uploads/2016/06/XIII6614-800x800.jpg';

image = image.replace(/^(.*?)(-\d+x\d+)(\.[^/]+)$/, "$1$3");
console.log(image);    

You can use this regex, refering to matched groups when replacing.

Comments