const content = {
"canvasSize": "800*480",
"elements": [
{
"id": "0550CF87",
"type": "image",
"src": "20230526/img/988f3b66a1f34923b0801322a8f79906.png",
"width": 300,
"height": 300,
"left": 436.2053915055192,
"top": 22.88960271080322,
"fixedRatio": true,
"rotate": 0
},
{
"type": "text",
"id": "E5CD72E2",
"left": 64.30686172065481,
"top": 42.53744339951236,
"width": 278.64855451062346,
"height": 322.375,
"content": "<p style=\"\"><span style=\"font-size: 42px\"><span style=\"color: rgba(255, 0, 0, 1);\">诚信赢天下</span></span></p><p style=\"\"><span style=\"font-size: 42px\"><span style=\"color: rgba(255, 0, 0, 1);\">万事和为贵</span></span></p><p style=\"\"><span style=\"font-size: 42px\"><span style=\"color: rgba(255, 0, 0, 1);\">家和万事兴</span></span></p><p style=\"\"><span style=\"font-size: 42px\"><span style=\"color: rgba(255, 0, 0, 1);\">爱拼才会赢</span></span></p>",
"rotate": 0,
"defaultFontName": "Microsoft Yahei",
"defaultColor": "#000",
"lineHeight": 1.8
}
],
"background": {
"type": "solid",
"color": "rgba(255, 255, 255, 1)"
}
}
//提取字符
let txt = getTextContent(content);
//提取段落p
let p = getTextToP(content)
console.log(txt,p)
//改变后的文字
let txt2 = '床前明月光\n疑是地上霜\n举头望明月\n低头思故乡'
txt2 = txt2.split('\n')
console.log(txt2)
const p2 =txt2.map(e=>{
return p[0].replace('@Txt',e)
})
console.log(p2)
//提取所有字符
function getTextContent (content){
const txtArr = content.elements.filter((e) => e.type == 'text')
return txtArr.map((e) => {
const regex = /<[^>]*>([^<]*)<\/[^>]*>/g
let matches = e.content.match(regex)
return matches
.map(match => match.replace(/<\/?[^>]+(>|$)/g, ''))
.filter(Boolean)
.join('\n')
// e.content.replace(/<\/?.+?>/g, '')
})
}
//提取p
function getTextToP (content){
const txtArr = content.elements.filter((e) => e.type == 'text')
return txtArr.map((e) => {
const regex = /<p[^>]*>.*?<\/p>/g;
const paragraphs = e.content.match(regex);
const txtArr = paragraphs[0].replace(/<\/?.+?>/g, '')
return paragraphs[0].replace(txtArr, '@Txt')
})
}
console