getImageAndVideoURL (con) {
let obj = {}
const imgReg = /<img.*?(?:>|\/>)/gi // 匹配图片中的img标签
const videoReg = /<video(([\s\S])*?)<\/video>/gi // 匹配图片中的img标签
const srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i // 匹配图片中的src
const ImageArr = con.match(imgReg) || [] // 筛选出所有的img
const VideoArr = con.match(videoReg) || [] // 筛选出所有的video
const indexImg = con.search(imgReg)
const indexVideo = con.search(videoReg)
// 去除所有图片和视频后的帖子内容
const narrowHandle = con.replace(/ /gi, '').replace(/<img.*?(?:>|\/>)/gi, '').replace(/<video(([\s\S])*?)<\/video>/gi, '').replace(/<p(.*)><\/p>/gi, '').replace(/<[^<>]+>/g, '')
const ImageSrcArr = [] // 图片地址数组
const VideoSrcArr = [] // 视频地址数组
for (let i = 0; i < ImageArr.length; i++) {
const imgSrc = ImageArr[i].match(srcReg)
// 获取图片地址
ImageSrcArr.push(imgSrc[1])
}
for (let i = 0; i < VideoArr.length; i++) {
const videoSrc = VideoArr[i].match(srcReg)
// 获取图片地址
VideoSrcArr.push(videoSrc[1])
}
let haveImageOrVideo = false
let showImageOrVideo = ''
if (indexImg === -1 && indexVideo === -1) {
haveImageOrVideo = false
} else if (indexImg > 0 && indexImg < indexVideo) {
haveImageOrVideo = true
showImageOrVideo = 'IMG'
} else if (indexImg > 0 && indexVideo < 0) {
haveImageOrVideo = true
showImageOrVideo = 'IMG'
} else if (indexVideo > 0 && indexVideo < indexImg) {
haveImageOrVideo = true
showImageOrVideo = 'VIDEO'
} else if (indexVideo > 0 && indexImg < 0) {
haveImageOrVideo = true
showImageOrVideo = 'VIDEO'
}
obj = {
ImageSrcArr,
VideoSrcArr,
haveImageOrVideo,
showImageOrVideo,
narrowHandle,
showLeft: true
}
return obj
}