this.huiy = this.dealTxt(this.info.contentInfo) // 富文本字段
dealTxt(htmlStr) {
console.log('htmlStr', htmlStr) // 从接口获取到富文本内容
let apiSource = 'https://8080'; // 地址端口
const _imgL = '<img src="';
const _video = '<video src="'
const _href = '<a href="'
if (htmlStr) {
// 解析富文本,获取所有图片标签或链接
const regex = /<img.*?src=["'](.*?)["']/g;
const images = [];
let match;
while ((match = regex.exec(htmlStr)) !== null) {
images.push(match[1]);
}
// 遍历每个图片链接,判断是否有前缀
for (const image of images) {
if (image.startsWith('http://') || image.startsWith('https://')) {
console.log(`${image} 包含前缀`);
} else {
console.log(`${image} 不包含前缀`);
// 给没有前缀的图片URL添加apiSource前缀
if (htmlStr.includes(_imgL)) {
htmlStr = htmlStr.replace(/<img src=\"/g,
'<img style="width:100%;height:auto" class="ui-iva" src="' + apiSource)
console.log(htmlStr.includes(_imgL), '=====')
}
}
}
if (htmlStr.includes(_video)) {
htmlStr = htmlStr.replace(/<source src=\"/g, '<source src="ui-iva' + apiSource)
}
if (htmlStr.includes(_href)) {
htmlStr = htmlStr.replace(/<a href=\"/g, '<a href="' + apiSource)
}
}
console.log('htmlStr', htmlStr)
return htmlStr;
},
2.
dealTxt(htmlStr) {
let apiSource = this.risun.info.source; // 地址端口
const imgRegex = /<img[^>]+src="([^">]+)"/g;
const videoRegex = /<video[^>]*>/g;
const sourceRegex = /<source[^>]+src="([^">]+)"/g;
const hrefRegex = /<a[^>]+href="([^">]+)"/g;
if (htmlStr) {
htmlStr = htmlStr.replace(imgRegex, (match, src) => {
return `<img src="${apiSource}${src}"`;
});
htmlStr = htmlStr.replace(videoRegex, '');
htmlStr = htmlStr.replace(sourceRegex, (match, src) => {
return `<video style="width:100%;" src="${apiSource}${src}"`;
});
htmlStr = htmlStr.replace(hrefRegex, (match, href) => {
return `<a href="${apiSource}${href}"`;
});
}
return htmlStr;
},