我想提取html中的img的src中的内容,匹配是否有img,如果有全部的src都提取出来,请大神赐教。(用java处理)
这个是测试图片1<img src="file:///storage/emulated/0/DCIM/Screenshots/Screenshot_hahaha.png" alt="">
这个是测试图片2<img src="file:///storage/emulated/0/DCIM/Screenshots/Screenshot_lalala.png" alt="">
大家讲道理2017-04-18 10:53:26
正規表示式來實現,當然我使用php
語言實作
$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=\"(.*?)\"/";
if(preg_match_all($imgreg, $body, $matches)){
var_dump($matches[1]);
}
PHP中文网2017-04-18 10:53:26
把樓上代碼轉換成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=\"(.*?)\"/g
var match
while((match = imgReg.exec(body))){
sources.push(match[1])
}
console.log(sources)