0
我有以下功能對單個字符串inputHtml
執行多個替換操作。它運作良好,但需要很長時間。是否可以通過組合它們來加速它?Javascript:在單個字符串中執行多個「替換」
/* Receives HTML code and returns the plain text contained in the HTML code */
function decodeHtml(inputHtml) {
const commentsRemoved = inputHtml.replace(/<!--[\s\S]*?-->/gm, '');
const linebreaksAdded = commentsRemoved.replace(/<br>/gm, '\n');
const tagsRemoved = linebreaksAdded.replace(/<(?:.|\n)*?>/gm, '');
const linebreaksRemoved = tagsRemoved.replace(/^\s*[\r\n]/gm, '');
const plainText = entities.decode(linebreaksRemoved);
return plainText;
}
這當然有可能!你有什麼嘗試? –
'(?:。| \ n)*?'是真正減慢速度的原因。已經將它固定爲'[\ s \ S] *?'會給你一些提升。 –
你使用nodejs嗎? –