測試用富文本如下:
<p style="border: 0px; margin-top: 0.63em; margin-bottom: 1.8em; padding: 0px; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; color: rgb(25, 25, 25); font-family: "PingFang SC", Arial, 微軟雅黑, 宋體, simsun, sans-serif; white-space: normal; background-color: rgb(255, 255, 255); text-align: justify;"><span style="border: 0px; margin: 0px; padding: 0px;">這是一段富文本</span></p>
使用 uni-app 的 rich-text 組件直接渲染會拋出 Vue 引擎解析的錯誤,原因是 font-family 的值里面包含了雙引號,此時需要先處理下引號嵌套的問題再使用。
function formatQuoteInStyle(htmlString) {
var stylePattern = /style="[^=>]*"[\s+\w+=|>]/g;
var innerQuotePattern = /(?<!=)"(?!>)/g;
return htmlString.replace(stylePattern, function(matches) {
return matches.replace(innerQuotePattern, '\'');
});
}
上述方法會將 style 屬性值里面的雙引號轉換為單引號,這樣就可以繼續執行下去了。