473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Complex regex to alter img tags

Using the WYSIWYG contenteditable feature, Internet Explorer will often
add a style to the image tag to define its display size after the image
has been dragged to display at something other than its natural size.
For example:

style="WIDTH: 432px; HEIGHT: 344px"

The values contained within the style are the correct ones resulting
from the drag and override the standard img tag height= and width=
parameters still remaining in the image tag.

I am seeking a regex that will read the values within the added style
portion of the tag, remove the style parameter, and leaving everything
else the same rewrite the tag with assignment of the width and height
values within the style to the standard width= and height= parameters.
For example, change this:

<IMG style="WIDTH: 432px; HEIGHT: 344px" height=442 hspace=10
src='http://theimgurl/filename.jpg"; width=612 align=middle vspace=10
border=0>

to this:

<IMG height=344 hspace=10 src='http://theimgurl/filename.jpg";
width=432 align=middle vspace=10 border=0>

Will that be possible? It seems like it would be a very complex regex
well beyond my skills and possibly my brainpower but I'd be willing to
give it a try if it is technically possible.

The purpose is that under certain circumstances the presence of the
style breaks page layouts in older browsers. I'm not sure, but also if
a browser does not have styles on then the image would appear in the
unintended size, right?

Thanks!

Rob

Jul 23 '05 #1
5 2255
bl*******@aol.com wrote:
Using the WYSIWYG contenteditable feature, Internet Explorer will often
add a style to the image tag to define its display size after the image
has been dragged to display at something other than its natural size.
For example:

style="WIDTH: 432px; HEIGHT: 344px"

The values contained within the style are the correct ones resulting
from the drag and override the standard img tag height= and width=
parameters still remaining in the image tag.

I am seeking a regex that will read the values within the added style
portion of the tag, remove the style parameter, and leaving everything
else the same rewrite the tag with assignment of the width and height
values within the style to the standard width= and height= parameters.
Then all that is required is to remove the style attributes via
DOM, there is no need for any RegExp.
For example, change this:

<IMG style="WIDTH: 432px; HEIGHT: 344px" height=442 hspace=10
src='http://theimgurl/filename.jpg"; width=612 align=middle vspace=10
border=0>


The following removes the style width & height attributes when
the image is clicked on, you may wish to do it from some other
event but the method is the same:

<IMG style="WIDTH: 432px; HEIGHT: 344px" height="442"
hspace="10" src="http://theimgurl/filename.jpg";
width="612" align="middle" vspace="10" border="0"
onclick="
this.style.height=null;
this.style.width=null;
">

Load the image, it will have the size given by the style
attributes, when clicked on, these are removed and the image
returns to the dimensions set by the image's width & height
attributes.

[...]
--
Rob
Jul 23 '05 #2
JRS: In article <11**********************@o13g2000cwo.googlegroups .com>
, dated Tue, 22 Mar 2005 20:22:13, seen in news:comp.lang.javascript,
bl*******@aol.com posted :

I am seeking a regex that will read the values within the added style
portion of the tag, remove the style parameter, and leaving everything
else the same rewrite the tag with assignment of the width and height
values within the style to the standard width= and height= parameters.
For example, change this:

<IMG style="WIDTH: 432px; HEIGHT: 344px" height=442 hspace=10
src='http://theimgurl/filename.jpg"; width=612 align=middle vspace=10
border=0>

to this:

<IMG height=344 hspace=10 src='http://theimgurl/filename.jpg";
width=432 align=middle vspace=10 border=0>

Will that be possible? It seems like it would be a very complex regex
well beyond my skills and possibly my brainpower but I'd be willing to
give it a try if it is technically possible.

You have not said whether your posting agent and/or technique have split
the <IMG ... > over more than one line. If that really is on one line,
then the following MiniTrue command line may be close to what you want
(it will be sent as a single line; how you see it is up to you and your
system) :

mtr -x+ $T.0 - "(<IMG) style=\x22WIDTH: (\d+)px; HEIGHT: (\d+)px\x22(.*height=)\d+(.*width=)\d+" = \1\4\3\5\2

mtr is MiniTrue
-x+ (re-)enables RegExps;
$T.0 is a file containing much of your article
- is a separator
" are delimiters
= is a separator
\x22 stands for "
Pieces that you want to keep are in parentheses
..* means anything
\d+ means a number
\1 \4 \3 \5 \2 are back-references
It is assumed that WIDTH HEIGHT height width are always in that order;
if not, consider two passes.
If the original was on more than one line, I don't have time to deal with
that tonight; but it should not be an insuperable problem (especially if
always similarly split).

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
free, DOS/Win/UNIX, <URL:http://www.idiotsdelight.net/minitrue/> Update hope?
Jul 23 '05 #3

RobG wrote:
bl*******@aol.com wrote:
Using the WYSIWYG contenteditable feature, Internet Explorer will often add a style to the image tag to define its display size after the image has been dragged to display at something other than its natural size. For example:

style="WIDTH: 432px; HEIGHT: 344px"

The values contained within the style are the correct ones resulting from the drag and override the standard img tag height= and width=
parameters still remaining in the image tag.

I am seeking a regex that will read the values within the added style portion of the tag, remove the style parameter, and leaving everything else the same rewrite the tag with assignment of the width and height values within the style to the standard width= and height= parameters.

Then all that is required is to remove the style attributes via
DOM, there is no need for any RegExp.
For example, change this:

<IMG style="WIDTH: 432px; HEIGHT: 344px" height=442 hspace=10
src='http://theimgurl/filename.jpg"; width=612 align=middle

vspace=10 border=0>


The following removes the style width & height attributes when
the image is clicked on, you may wish to do it from some other
event but the method is the same:

<IMG style="WIDTH: 432px; HEIGHT: 344px" height="442"
hspace="10" src="http://theimgurl/filename.jpg";
width="612" align="middle" vspace="10" border="0"
onclick="
this.style.height=null;
this.style.width=null;
">

Load the image, it will have the size given by the style
attributes, when clicked on, these are removed and the image
returns to the dimensions set by the image's width & height
attributes.

[...]
--
Rob


Thank you, I do need the sizes that are stored in the style, but you've
showed me how to get to those. Perhaps I can just capture all the
other expected attributes in the image tag via the DOM then just write
a whole new tag in place of the original. Should be easier than trying
to parse it.

Jul 23 '05 #4

Dr John Stockton wrote:
JRS: In article <11**********************@o13g2000cwo.googlegroups .com> , dated Tue, 22 Mar 2005 20:22:13, seen in news:comp.lang.javascript,
bl*******@aol.com posted :

I am seeking a regex that will read the values within the added styleportion of the tag, remove the style parameter, and leaving everythingelse the same rewrite the tag with assignment of the width and heightvalues within the style to the standard width= and height= parameters.For example, change this:

<IMG style="WIDTH: 432px; HEIGHT: 344px" height=442 hspace=10
src='http://theimgurl/filename.jpg"; width=612 align=middle vspace=10border=0>

to this:

<IMG height=344 hspace=10 src='http://theimgurl/filename.jpg";
width=432 align=middle vspace=10 border=0>

Will that be possible? It seems like it would be a very complex regexwell beyond my skills and possibly my brainpower but I'd be willing togive it a try if it is technically possible.

You have not said whether your posting agent and/or technique have

split the <IMG ... > over more than one line. If that really is on one line, then the following MiniTrue command line may be close to what you want (it will be sent as a single line; how you see it is up to you and your system) :

mtr -x+ $T.0 - "(<IMG) style=\x22WIDTH: (\d+)px; HEIGHT: (\d+)px\x22(.*height=)\d+(.*width=)\d+" = \1\4\3\5\2
mtr is MiniTrue
-x+ (re-)enables RegExps;
$T.0 is a file containing much of your article
- is a separator
" are delimiters
= is a separator
\x22 stands for "
Pieces that you want to keep are in parentheses
.* means anything
\d+ means a number
\1 \4 \3 \5 \2 are back-references
It is assumed that WIDTH HEIGHT height width are always in that order; if not, consider two passes.
If the original was on more than one line, I don't have time to deal with that tonight; but it should not be an insuperable problem (especially if always similarly split).

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. © Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links. I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt; free, DOS/Win/UNIX, <URL:http://www.idiotsdelight.net/minitrue/>

Update hope?

Thank you sir. I'll need to study what are and how to use
backreferences. That seems to be key.

Jul 23 '05 #5
JRS: In article <11**********************@o13g2000cwo.googlegroups .com>
, dated Wed, 23 Mar 2005 21:32:03, seen in news:comp.lang.javascript,
bl*******@aol.com posted :

Thank you sir. I'll need to study what are and how to use
backreferences. That seems to be key.


Study also the newsgroup FAQ section 3.2.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dean H. Saxe | last post by:
I'm currently developing a tool in perl to search out potential XSS (Cross Site Scripting) vulnerabilities and correct them in a ColdFusion based web app. I've been having great success so far,...
8
by: G. Stewart | last post by:
The objective is to extract the first n characters of text from an HTML block. I wish to preserve all HTML (links, formatting etc.), and at the same time, extend the size of the block to ensure...
2
by: JKJ | last post by:
I need help with a regular expression that will pull the title and all the meta tags held in the head section of an HTML file (including the head tags). I want to exclude everything else such as...
3
by: Natalia DeBow | last post by:
Hi there, I have another question for .NET RegEx experts. I am reading in a C Sharp file line by line and I am trying to detect comments that start with either // of ///. What I am...
3
by: DDK | last post by:
I am trying to figure out how to Replace tags such as ... with the correct HTML <b>...</b> tags in C#. The code below works however only if one set of tags are found, if you have more than two...
1
by: darrel | last post by:
I have some vb.net code that is running a regex, matching groups, and replacing them. I'm trying to come up with a simple script that will strip all attributes from all HTML tags. This is what I...
13
by: Chris Lieb | last post by:
I am trying to write a regex that will parse BBcode into HTML using JavaScript. Everything was going smoothly using the string class replace() operator with regex's until I got to the list tag....
7
by: MrNobody | last post by:
I'm trying to do some regex in C# but for some reason linebreaks are causing my regex to not work. the test string goes like this: string ss = "<tagname...
5
by: GS | last post by:
what is a good regex expression for remove html <img ....tag? I tried "<img *\->", RegexOptions.IgnoreCase) but it is not quite working thank you for your time
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.