简体   繁体   中英

Get all strings between two patterns in Node.js

On my node.js server, I have a string, which contains html (unescaped) and a few (not only one, but more) img tags with base64 src and I need to get all base64 strings in an array from it and than decode those base64's into images, save them somewhere on the server and replace base64 strings with img address, for example I need to change this

<img src="data:image/png;base64,iVBORw...some-pretty-damn-long-string...K5CYII=" style="foo:bar;" possible-other-attribude="baz"/>

to this:

<img src="/media/images/filename.png" style="foo:bar;" possible-other-attribude="baz"/>

I have no problem with the actual decoding, saving to .png and than replacing in the string, but I don't know how to get the Array with all base64 strings. Thanks for help.

var strings = htmlCode.match(/src="data:[a-z\/]+;base64,.+?"/g)

for(var i = 0; i < strings.length; i++)
{
    doSomething(strings[i].match(/,(.+?)"/)[1]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM