1
votes

we have data coming from another application(DB) like below image file has been stored like

@1="< img width=498 height=425 src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ...">"

var data = @1 DownloadCode();

i want to download file image file. I am newbie in JS.

1
Not too sure ! do you want a physical image? As I can see you have base64 encoded image. So if you want the physical image you need to decode it. - MMRahman
i tried this code. i need src of image in js variable ........var m, urls = [], str = '<img width=498 height=425 src="data:image/jpeg;base64,/9j/4AAQSkZJRgA...">', rex = /<img[^>]+src="?([^"\s]+)"?\s*\/>/g; while ( m = rex.exec( str ) ) { urls.push( m[1] ); } console.log( urls ); - Rahul Patel
The code looks good. and putting everything into an array. you just need to take it from an array ... If you are not using multiple Images avoid array and put into variable - MMRahman
my string is like str = '<img width=498 height=425 src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//gAcU29mdHdhcmU6IE1p......hl//2Q">' this reg ex is not working for me. img tag is not closed properly. i think. It is able to render correct. - Rahul Patel
Please Check my answer and let me know if it works for you. - MMRahman

1 Answers

1
votes

Here how you should do it

var str = '<img width=498 height=425 src="data:image/jpeg;base64,/9j/4AAQSkZJRgA...">';

var regex = /<img.*?src="(.*?)"/;
var src = regex.exec(str)[1];

console.log(src);