java - 正則表達(dá)式匹配多次出現(xiàn)的內(nèi)容,并且提取
問題描述
我想提取html中的img的src中的內(nèi)容,匹配是否有img,如果有全部的src都提取出來,請(qǐng)大神賜教。(用java處理)
這個(gè)是測(cè)試圖片1<img src='file:///storage/emulated/0/DCIM/Screenshots/Screenshot_hahaha.png' alt=''>這個(gè)是測(cè)試圖片2<img src='file:///storage/emulated/0/DCIM/Screenshots/Screenshot_lalala.png' alt=''>
問題解答
回答1:正則表達(dá)式來實(shí)現(xiàn),當(dāng)然我使用php語言實(shí)現(xiàn)
$body = ’<img src='file:///storage/emulated/0/DCIM/Screenshots/Screenshot_hahaha.png' alt=''><img src='file:///storage/emulated/0/DCIM/Screenshots/Screenshot_lalala.png' alt=''>’;$imgreg = '/<img src='http://www.aoyou183.cn/wenda/(.*?)'/';if(preg_match_all($imgreg, $body, $matches)){ var_dump($matches[1]);}回答2:
把樓上代碼轉(zhuǎn)換成js
var body = ’<img src='file:///storage/emulated/0/DCIM/Screenshots/Screenshot_hahaha.png' alt=''><img src='file:///storage/emulated/0/DCIM/Screenshots/Screenshot_lalala.png' alt=''>’var sources = []var imgReg = /<img src='http://www.aoyou183.cn/wenda/(.*?)'/gvar matchwhile((match = imgReg.exec(body))){ sources.push(match[1])}console.log(sources)
