regex – Finding quote marks within alt tags – Education Career Blog

I’m wondering how I would approach the following problem with regular expressions. We run into the occasional problem of quote marks (“) inside Alt tags which can cause rendering issues. Would it be possible to write a regular expression to find Img tags, but only when the ALT contains quotes?

For example these would be found

<img src="theImage.gif" width="81" height="24" border="0" style="display:block;" alt="Check "it" out">
<img src="theImage.gif" width="81" height="24" alt="Check "it" out"  style="display:block;">

But not these

<img src="theImage.gif" width="81" height="24" border="0" style="display:block;" alt="Check 'it' out">
<img src="theImage.gif" width="81" height="24" border="0" style="display:block;" alt="">
<img src="theImage.gif" width="81" height="24" border="0" style="display:block;">

Thanks in advance!

,

This problem is intractable because you might end up with something like:

<img src="theImage.gif" width="81" height="24" alt="foo" border="bar"> 

Would you interpret that as an alt value of foo and a border of bar, or as an alt value of foo" border="bar?

This is why you must properly escape your data before rendering it into HTML. You can’t unstir a cup of tea.

,

The problem is likely that the attribute value needs to be HTML encoded when rendered.

Leave a Comment