D.B D.B - 1 year ago 81
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 Source
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\\,", "");

and the output is

<img src="/9j/.....
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download