// 预防 XSS 攻击的安全转义函数
function escapeHTML(str) {
return str.replace(/[&<>"']/g, function(match) {
switch(match) {
case '&': return '&';
case '<': return '<';
case '>': return '>';
case '"': return '"';
case "'": return ''';
default: return match;
}
});
}
// 转义正则特殊字符
export function escapeRegExp(string: string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
// 关键词高亮
const regex = new RegExp(escapeRegExp(escapeHTML(keyword)), 'gi');
str = str.replaceAll(regex, match => `<span class=${styles.highlight}>${match}</span>`);