function escapeRegExp(string: string): string { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } function sanitizeInput(text: string): string { return text?.replace(//g, '>').replace(/"/g, '"'); } export function highlightText(text: string, keyword: string): string { const safeText = sanitizeInput(text); const safeKeyword = sanitizeInput(keyword); if (!text || typeof text !== 'string') { return text; } if (!safeKeyword || typeof safeKeyword !== 'string') { return safeText; } const trimmedKeyword = safeKeyword.trim(); if (!trimmedKeyword) { return safeText; } try { const regex = new RegExp(`(${escapeRegExp(trimmedKeyword)})`, 'gi'); const parts = safeText.split(regex); if (parts.length === 1) { return safeText; } return parts .map((part, index) => { if (part.toLowerCase() === trimmedKeyword.toLowerCase()) { return `${part}`; } return part; }) .join(''); } catch (error) { console.warn('Error highlighting text:', error); return safeText; } }