Search code examples
c#htmlregextracking-pixel

C# How to find a 1x1 image tag in HTML


I would like to know how to find an image tag in a string of HTML that has dimensions of 1x1, basically I am looking for tracking pixels. So for example:

<img src=\"http://somewhere.com\" width=\"1\" height=\"1\" style=\"display:none!important;\">

My end goal is to be able to find this part of the code and remove it from the string.

I have read few a few posts already on how to find all img tags but that is not what I am looking for. I only want the img tags that are 1x1.

Can anyone help with this?


Solution

  • If you just want to remove this img tags from your string, you can do that with regexp:

     string result = Regex.Replace(html, "<img.+?(width|height)=[\"']1[\"'].+?(width|height)=[\"']1[\"'].*>", "", RegexOptions.IgnoreCase);