0

I have a base64 data

data:application/pdf;base64,JVBERi0xLj........

I want to get the rest of the data like

JVBERi0xLj........

Following not working, wonder what is the correct approach?

const express = /data:(.*?);base64,(.*?)/g;
const arr = express.exec(base64);
kenpeter
  • 7,404
  • 14
  • 64
  • 95
  • 2
    Remove the `?` in the last group if you want to match the rest of the line. Currently it is non greedy but there is nothing following in the pattern so it will match as least as possible. – The fourth bird Aug 17 '19 at 14:32

1 Answers1

0

You can simplify and match all the data uri standard :

:([^;]*)[^,]+,(.*)

Your Regex will not match other data urls that are also standard like :

data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh.
data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678

Paolo
  • 21,270
  • 6
  • 38
  • 69
YOGO
  • 531
  • 4
  • 5
  • if I want to capture the encoding method, do I do :([^;]*);([^,]+),(.*) – kenpeter Aug 18 '19 at 03:21
  • It will capture in my example foo=bar;base64, also in this standard there is only one accepted encoding method base64 to be declared, or url encoding like the example – YOGO Aug 18 '19 at 06:41
  • You should do :([^;]*);[^,]+(base64)?,(.*) group 2 will contain or not base64 if its encoded. – YOGO Aug 18 '19 at 07:01
  • To be a bit more in line with the standard : :([^;,]*)?[^,]*(base64)?,(.*) Will match other standard compliant url like : data:,thisisthedata – YOGO Aug 18 '19 at 07:37