JavaScript / ECMAScript

Data URI in JS

Match base64-encoded data URIs, including the MIME type and the base64 payload.

Try it in the JS tester →

Pattern

regexJS
data:[\w\/+\-.]+(?:;[\w=\-]+)*;base64,[A-Za-z0-9+\/=]+   (flags: gi)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("data:[\\w\\/+\\-.]+(?:;[\\w=\\-]+)*;base64,[A-Za-z0-9+\\/=]+", "gi");
const input = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

Starts with data:, then a MIME type (word chars, slashes, plus, hyphen, dot), optional parameters, then ;base64, and the base64 payload (letters, digits, +, /, =).

Examples

Input

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA

Matches

  • data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA

Same pattern, other engines

← Back to Data URI overview (all engines)