开发者

Finding quote marks within alt tags

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="the开发者_开发百科Image.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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜