user3929607
user3929607

Reputation: 231

replace strings in url using regular expression

I am really poor in regular expressions, So that is the reason i am asking the basic question here. I want to change some strings with slashes in a url in javascript. Please help me to get out from this.

This is my url

http://mysite.local/media/catalog/product/cache/1/thumbnail/56x/qewewq1312321dfde5fb8d27136e95/m/u/music6_1_1.jpg

And i want to replace 'thumbnail/56x' with 'image' like

http://mysite.local/media/catalog/product/cache/1/image/qewewq1312321dfde5fb8d27136e95/m/u/music6_1_1.jpg

How can i get with regular expression?

Upvotes: 0

Views: 1165

Answers (2)

Harish Ambady
Harish Ambady

Reputation: 13121

Just use Javascript string replace function.

Try following:

var str = "http://mysite.local/media/catalog/product/cache/1/thumbnail/56x/qewewq1312321dfde5fb8d27136e95/m/u/music6_1_1.jpg";
var res = str.replace("thumbnail/56x", "image");

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174716

thumbnail\/56x regex would replace the exact thumbnail/56x part in your link with image.

> "http://mysite.local/media/catalog/product/cache/1/thumbnail/56x/qewewq1312321dfde5fb8d27136e95/m/u/music6_1_1.jpg".replace(/thumbnail\/56x/g, "image")
'http://mysite.local/media/catalog/product/cache/1/image/qewewq1312321dfde5fb8d27136e95/m/u/music6_1_1.jpg'

thumbnail\/\d+x regex would replace any number in the thumbnail part like thumbnail/673px with image.

> "http://mysite.local/media/catalog/product/cache/1/thumbnail/56x/qewewq1312321dfde5fb8d27136e95/m/u/music6_1_1.jpg".replace(/thumbnail\/\d+x/g, "image")
'http://mysite.local/media/catalog/product/cache/1/image/qewewq1312321dfde5fb8d27136e95/m/u/music6_1_1.jpg'

Upvotes: 1

Related Questions