2

I am fetching HTML content from database and displaying in UI. If the displayed content contains images having width and height specified then I need to replace the image width and height to some fixed value while displaying in UI.

One sample content is like:

<p>
   Sample paragraph text. 
   <br />
   <img src="http://www.example.com/myimage.jpg?width=500&height=500" 
    title="Test Image" />
   Sample text.
   <br />
</p>

Here, I want to set the image width=300 and remove the height dynamically for different images.

I have tried to replace the text using regular expression as follows:

.replace(new RegExp("width=([^&]{0,})", 'gi'), 'width=300') 
.replace(new RegExp("[?&]height=([^&]{0,})", 'gi'), '')

But it gives result as follows:

<p>Sample paragraph text. <br /> <img src="http://www.example.com/myimage.jpg?width=300

It replaces all the text after ?width=300.

I want the result like: <p>Sample paragraph text. <br /> <img src="http://www.example.com/myimage.jpg?width=300" title="Test Image" /> Sample text.<br /></p>

So, I guess, the problem is in the regular expression. Please help me to correct this.

Many many thanks in advance!

4
  • 1
    Parse the HTML first and only apply the string substitution on the attribute value you want to change (I assume you know how to traverse the DOM). Commented Sep 4, 2013 at 7:04
  • 1
    The browser parses the HTML, why not set innerHTML and then change the image dimensions ? Commented Sep 4, 2013 at 7:04
  • Why don't you try exact regex match /^width=([^&]{0,})$/ Commented Sep 4, 2013 at 7:04
  • @Dystroy: I am using ImageResizer to set the image width and height. ImageResizer handles the width and height if it is specified in image "src" attribute. imageresizing.net/docs/basics Commented Sep 4, 2013 at 7:16

2 Answers 2

1

This pattern: /[^&]{0,}/ is gobbling up everything that is not a & character, which is why it extends beyond the URL itself. Instead, since you know that the width and height are numbers, you can just match on digits:

.replace(/\bwidth=\d*/gi,'width=300')
.replace(/&height=\d*|\bheight=\d*&?/gi,'');

The \b is a zero-width match to a word boundary, so that if you have framewidth=500 for example it won't be affected. The 2 matches to height are so that it won't replace both &'s if they are on each side. E.g. you don't want '?width=500&height=500&cache=123' to turn into '?width=300cache=123' for instance.

Sign up to request clarification or add additional context in comments.

Comments

0

Unless you have more parameters in the query, this should do it:

.replace(/(\?width=)[^"]+/i, '$1300');

Put this in a console to test it:

"http://www.example.com/myimage.jpg?width=500&height=500".replace(/(\?width=)[^"]+/i, '$1300');

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.