// 解析HTML
export const analysis = function (str) {
if (!str) {
return ''
}
// 处理标签
let text = replaceTag(str);
// 处理特殊字符
text = stripscript(text);
// 处理回车符,反斜杠
text = stripscript1(text);
return text;
}
// 处理获取dom元素内容
export const handleText = function (el) {
try {
if (!el.innerText) {
return ""
}
let text = ""
const innerText = el.innerText
const innerHtml = el.innerHtml
if(innerText){
text = analysis(innerText);
} else if(innerHtml) {
text = analysis(innerHtml);
}
return text;
} catch (error) {
console.log("error", error)
return ""
}
}
// 处理标签
function replaceTag(str) {
return str.replace(/<.*?>/g, "");
}
// 处理特殊字符
function stripscript(s) {
var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]")
var rs = "";
for (var i = 0; i < s.length; i++) {
rs = rs + s.substr(i, 1).replace(pattern, '');
}
return rs;
}
// 处理特殊字符
function stripscript1(str) {
return str.replace(/(\n|\r|\r\n|↵)/g, '')
}