D.B D.B - 2 months ago 13
C# Question

Regular Expression to remove the first section from src attribute of an image

I need a regular expression able to replace the first section from src property of an image.

The image html looks like this

var example="<img src=\"data:image/jpeg;base64,/9j/....."


I need to replace all characters before the first comma (,) and replace it for an empty string, the "jpeg" section should be generic.

I tried that one but didn't work:

string res = example.Replace(@"/^data:image\/\w +\;base64,", "");

Answer
public class Test
{
    public static void Main()
    {
        var example="<img src=\"data:image/jpeg;base64,/9j/.....";
        string res = Regex.Replace(example, "data:image\\/\\w+\\;base64\\,", "");
        Console.WriteLine(res);
    }
}

and the output is

<img src="/9j/.....