ncyoung.com

regular expression for closing image tags

This entry is in the following categories:

Top->Programming->Perl
 - Older in Perl: installing LibXSLT on windows using PPM
 - Jump to oldest entry: perl for drawing ascii tables and charts

Top->Programming->Web
 - Older in Web: XHTML Appendix
 - Newer in Web: a smattering of web dev links

Top->Programming->XML
 - Older in XML: XHTML Appendix
 - Jump to oldest entry: schematron is so cool

You can use this regular expression to find image tags that are not self closing and make them self closing. It will change:

<img src="spacer.gif" height="1" width="1">

to:

<img src="spacer.gif" height="1" width="1"/>

If you have:

<img src="spacer.gif" height="1" width="1"></img>

It will STILL change it to:

<img src="spacer.gif" height="1" width="1"/></img>

So in that case you can't just replace all.

It should be easy to re-purpose this for other tags that need to be made self closing.

The find term is:

<img ([^>]*)([^/])>

The replace term is:

<img \1\2/>




Dated: 05/16/2006