const abstractImagesFromHTML = ( str ) => { let imgReg = /<img(?:(?:".*?")|(?:'.*?')|(?:[^>]*?))*>/gi ; let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i ; let result = [ ] ; let img ; while ( img = imgReg . exec ( str ) ) { let _img = img [ 0 ] . match ( srcReg ) ; if ( _img && _img [ 1 ] ) result . push ( _img [ 1 ] ) ; } return result ; } ; param { [ type ] } html return { [ type ] } const htmlToString = ( html ) => { let imgReg = /<img(.*?)>/gi ; let imgs = [ ] ; let img ; while ( img = imgReg . exec ( html ) ) { imgs . push ( img [ 0 ] ) ; } imgs . map ( item => { html = html . replace ( item , '[图片] ' ) ; } ) ; html = html . replace ( /<[^>]+>/g , "" ) ; return html ; } ; const htmlImgToText = ( html ) => { let imgReg = /<img(.*?)>/gi ; let imgs = [ ] ; let img ; while ( img = imgReg . exec ( html ) ) { imgs . push ( img [ 0 ] ) ; } imgs . map ( item => { html = html . replace ( item , '[图片] ' ) ; } ) ; return html ; } ; const htmlImageOptimization = ( str ) => { let imgReg = /<img(?:(?:".*?")|(?:'.*?')|(?:[^>]*?))*>/gi ; let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i ; let img ; while ( img = imgReg . exec ( str ) ) { let oldImgDom = img [ 0 ] ; if ( oldImgDom ) { let _img = oldImgDom . match ( srcReg ) ; if ( _img && _img [ 1 ] ) { let newImg = oldImgDom . replace ( _img [ 1 ] , _img [ 1 ] + '?imageView2/2/w/800/auto-orient/format/jpg' ) ; str = str . replace ( oldImgDom , newImg ) ; } } } return str ; } ; export default { abstractImagesFromHTML , htmlToString , htmlImgToText , htmlImageOptimization } ;