我需要基于contenteditable div文本在contenteditable div中添加div。示例案例:
const ceDiv = document.getElementById('foo')
//and when text in ceDiv ends with {{ i need to show another div in current caret position
类似于Facebook提到朋友的事情
我现在所做的是将div {{var}}替换为div:
export function findAndReplaceBrackets(string: string): string {
const bracketReg = /\{{2}(.*?)\}{2}/ig
const brackets = string.match(bracketReg)
if (!brackets) return string
string = string.replace(/<\/?[^>]+(>|$)/g, '') // reset variables in string
brackets.forEach(variable => {
string = string.replace(variable, `<span style="
cursor: pointer;
background-color: blue;
color: #ffffff;
text-transform: uppercase;
font-size: 13px;
padding: 1px;
margin: 0 2px;
border-radius: 4px
">${variable}</span>`)
})
return string
}
但这根本不能解决我的问题