先上方法
// 根据正则在字符串中查找结果
function getData (inStr, inPattern,inList = []){
let reg = inPattern
let res = inStr.match(reg);
// 匹配不到东西了
if(res == null) return;
// 匹配到的那个结果
let matchStr = res[0];
// 匹配位置
let matchIndex = inStr.indexOf(matchStr)
if(res){
inList.push(res[0])
if((res[0].length + matchIndex) < inStr.length){
let newStr = inStr.slice(res[0].length + matchIndex)
getData(newStr,inPattern,inList)
}
}
return inList;
}
测试一下
let str = '<p>这是一段文字</p><p><video src="http://tanda.zxyun119.com/admin/file/full/download/GQm0rxUJry/mp4" controls="controls" style="max-width:100%"></video></p><p>这是第二段文字</p><p><video src="http://tanda.zxyun119.com/admin/file/full/download/WZAp2DfEaM/mp4" controls="controls" style="max-width:100%"></video></p><p>这是第三段文本</p><p><iframe src="http://mpvideo.qpic.cn/0bf2zahsaaapx4ags2u5pfpv7sgdedea6iaa.f10002.mp4?dis_k=5c4cfea958eb9dbd786cd941bd0e8a4c&dis_t=1636358558&vid=wxv_1574211423902187523&format_id=10002&support_redirect=0&mmversion=false"></iframe></p>',
reg = /<video.*?>.*?<\/video>|<iframe.*?>.*?<\/iframe>/;
let list = genData(str, reg)
/**
[
'<video src="http://tanda.zxyun119.com/admin/file/full/download/GQm0rxUJry/mp4" controls="controls" style="max-width:100%"></video>',
'<video src="http://tanda.zxyun119.com/admin/file/full/download/WZAp2DfEaM/mp4" controls="controls" style="max-width:100%"></video>',
'<iframe src="http://mpvideo.qpic.cn/0bf2zahsaaapx4ags2u5pfpv7sgdedea6iaa.f10002.mp4?dis_k=5c4cfea958eb9dbd786cd941bd0e8a4c&dis_t=1636358558&vid=wxv_1574211423902187523&format_id=10002&support_redirect=0&mmversion=false"></iframe>'
]
*/