RSS Amplifier

(untitled) · Dec 4, 2014

How to extract the MIME type from a base64 string.

0
Sign in to vote or save

This site does not allow itself to be embedded. You can still read it on the original site — the toolbar below keeps your place in the directory.

Extract the MIME type from a base64 string using a regular expression. function base64MimeType(encoded) { var result = null ; if ( typeof encoded !== 'string' ) { return result; } var mime = encoded.match( /data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/ ); if (mime && mime.length) { result = mime[ 1 ]; } return result; } Usage: var encoded = 'data:image/png;base64,iVBORw0KGgoAA...5CYII=' ;…

Extract the MIME type from a base64 string using a regular expression.

function base64MimeType(encoded) {
 var result = null;

 if (typeof encoded !== 'string') {
 return result;
 }

 var mime = encoded.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/);

 if (mime && mime.length) {
 result = mime[1];
 }

 return result;
}

Usage:

var encoded = 'data:image/png;base64,iVBORw0KGgoAA...5CYII=';

console.log(base64Mime(encoded)); // "image/png"
console.log(base64Mime('garbage')); // null

On github at miguelmota/base64mime

Read on miguelmota.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.