How can I strip the data:image part from a base64 string of any image type in Javascript -


i doing following decode base64 images in javascript:

    var strimage = "";     strimage = strtoreplace.replace("data:image/jpeg;base64,", "");     strimage = strtoreplace.replace("data:image/png;base64,", "");     strimage = strtoreplace.replace("data:image/gif;base64,", "");     strimage = strtoreplace.replace("data:image/bmp;base64,", ""); 

as can see above accepting 4 standard image types (jpeg, png, gif, bmp);

however, of these images large , scanning through each 1 4-5 times replace seems dreadful waste , terribly inefficient.

is there way reliably strip data:image part of base64 image string in single pass?

perhaps detecting first comma in string?

thanks in advance.

you can use regular expression:

var strimage = strtoreplace.replace(/^data:image\/[a-z]+;base64,/, ""); 

  • ^ means at start of string
  • data:image means data:image
  • \/ means /
  • [a-z]+ means one or more characters between , z
  • ;base64, means ;base64,

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -