序文:
js は、リッチ テキスト文字列に子がいくつあるかに関係なく、指定された文字が別の文字に置き換えられることを認識します。
実装手順:
呼び出し方法:
this.nowHtmlT = this.replaceText(this.nowHtmlT,this.addBtnText,"${one}")
梱包方法:
replaceText(text, oldStr, newStr) {
// 检查是否为字符串类型
if (typeof text !== 'string') {
text = String(text);
}
// 替换字符
text = text.replace(new RegExp(oldStr, "g"), newStr);
// 处理子级
if (text.includes('<') && text.includes('>')) {
const start = text.indexOf('<');
const end = text.indexOf('>') + 1;
let subtext = text.substring(start, end);
while (start >= 0 && end >= 0 && end > start) {
const subtextNew = replaceText(subtext, oldStr, newStr);
text = text.substring(0, start) + subtextNew + text.substring(end);
subtext = text.substring(start, end);
}
}
return text;
},