java - 正则表达式匹配多次出现的内容,并且提取
问题描述
我想提取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=''>
问题解答
回答1:正则表达式来实现,当然我使用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='https://www.haobala.com/wenda/(.*?)'/';if(preg_match_all($imgreg, $body, $matches)){ var_dump($matches[1]);}回答2:
把楼上代码转换成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='https://www.haobala.com/wenda/(.*?)'/gvar matchwhile((match = imgReg.exec(body))){ sources.push(match[1])}console.log(sources)
