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!
/^width=([^&]{0,})$/