使用正则表达式获取富文本中的第一张图片
utils/validator.js
// 获取富文本第一张图片
export function haveImg(str) {
const arr = [];
if (typeof str === "string") {
try {
str.replace(
/<img [^>]*src=['"]([^'"]+)[^>]*>/,
function (match, capture) {
arr.push(capture);
}
);
// 获取所有图片
// str.replace(
// /<img [^>]*src=['"]([^'"]+)[^>]*>/g,
// function (match, capture) {
// arr.push(capture);
// }
// );
// if (arr.length > 0) {
// return arr;
// }
} catch (e) {
return false;
}
}
}