【发布时间】:2022-11-01 17:51:24
【问题描述】:
当标签没有关闭而不替换 HTML 字符时,有一种方法可以在 Javascript 中解析 HTML 中的字符 <?
谈论像<html>efrferrefrer<wedw 这样的字符串。
它必须回馈efrferrefrer<wedw。
尝试
function removeHtmlTags(input){
let tmp = document.createElement("div");
tmp.innerHTML = input;
return tmp.textContent || tmp.innerText || "";
}
//or
function removeHtmlTags(input){
return input.replace(/<[^>]*>?/gm, '');
}
没有给出预期的结果。
它消除了“<wedw”。
所以,有一种方法可以做到这一点不使用替换 html 字符的函数喜欢
function escapeHtml(text) {
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return = text.replace(/[&<>"']/g, function(m) { return map[m]; });
}
它必须完全是efrferrefrer<wedw。
【问题讨论】:
标签: javascript html html-parsing