Alma
Alma

Reputation: 4410

I replace the base 64 image with url

I have a string that contain the HTML and base 64 image, I separate the image by using Regax and upload it, I have a file path that I need to replace in image tag to return.

This is where I separate base 64 image:

    var base64image = Regex.Match(request.Note, "(?<=data:image/jpeg;base64,)[^\"]*");

and this is where I replace it.

  request.Note = request.Note.Replace(base64image.Value, url);

"<p>This is test </p><p><img src=\"data:image/jpeg;base64,https://files.test.com/qa/test.jpg\"></p>"

I have this extra: this: data:image/jpeg;base64, what is the best way to replace this?

Upvotes: 0

Views: 585

Answers (1)

Zay2k
Zay2k

Reputation: 138

You can use Regex Match.Groups Property

So something like this

var htmlImageMatches = Regex.Match(request.Note, "(data:image/jpeg;base64,)([^\"]*)");

var discardText = htmlImageMatches.Groups[1];
var base64image = htmlImageMatches.Groups[2];

request.Note = request.Note.Replace(base64image.Value, url);
request.Note = request.Note.Replace(discardText, "");

Upvotes: 2

Related Questions