function replaceHtml(html) {
let arr = [...html]
let startList = []
let endList = []
arr = arr.map((item, index) => {
if (item === '>') {
startList.push(index + 1)
} if (item === '<') {
endList.push(index)
}
return item
})
const arr2 = startList.map((item, index) => {
return arr.slice(startList[index], endList[index + 1])
})
const textList = arr2.reduce((pre, cur, index) => {
cur = cur.map((item) => {
return item === ' ' ? '$nbsp;' : item
})
const item = {
idx: index,
html: cur
}
pre.push(item)
return pre
}, [])
let arr3 = arr
textList.forEach((item, index) => {
if (item.html.length > 0) {
for (let i = 0; i < item.html.length; i++) {
let idx = startList[index] + i
arr3[idx] = item.html[i]
}
}
})
const htmlStr = arr3.toString().replace(/,/g, '')
return `${htmlStr}`
}
const content = `<bode>55<div style=" padding:1px">12 55 3</div></bode>`
const str = replaceHtml(content)
console.log(content);
console.log(str);
console