I have a base64 encoded image returned from a service and it looks like this:
/9j/4AAQSkZJRgABAQEASABIAAD/4Yp2aHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWN
This one will work for any extension type: pdf, mp3, png, ....
base64.substring(base64.indexOf('/') + 1, base64.indexOf(';base64'));