0

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?

1
  • Something like url.replace("thumbnail/56x", "image");. Fiddle. Commented Sep 18, 2014 at 5:26

2 Answers 2

1

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'
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.