Назад Зміст


Автопідсвітка всіх шаблонів одночасно

Текст автоматично підсвічує всі збіги для всіх шаблонів. Підсвітки зберігаються, можна скинути або завантажити текст з файлу.



const regexList = [
    { type: 'date', regex: /\b\d{2}\.\d{2}\.\d{4}\b/g },
    { type: 'phone', regex: /\+380\d{9}\b/g },
    { type: 'url', regex: /\bhttps?:\/\/[^\s]+/g },
    { type: 'digits', regex: /\b\d+\b/g },    
	{ type: 'capital', regex: /(^|[^А-Яа-яЁёЇїІіЄєҐґA-Za-z0-9_])([A-ZА-ЯЁЇІЄҐ][a-zа-яёіїєґA-Za-z]*)(?=[^А-Яа-яЁёЇїІіЄєҐґA-Za-z0-9_]|$)/g } 
];
let currentHTML = '';

function highlightAll() {
    const textArea = document.getElementById('textInput');
    const outputDiv = document.getElementById('output');
    const errorDiv = document.getElementById('error');
    errorDiv.textContent = '';

    try {
        let text = textArea.value;
        currentHTML = escapeHTML(text);

        regexList.forEach(item => {
            currentHTML = currentHTML.replace(item.regex, match => `<tmark class="${item.type}">${match}</mark>`);
        });

        outputDiv.innerHTML = currentHTML;
    } catch(e) {
        errorDiv.textContent = 'Помилка: ' + e.message;
    }
}

function resetHighlights() {
    const outputDiv = document.getElementById('output');
    outputDiv.innerHTML = '';
    currentHTML = '';
    document.getElementById('error').textContent = '';
}

function clearText() {
    document.getElementById('textInput').value = '';
    resetHighlights();
}

function loadTextFromFile() {
    const fileInput = document.getElementById('fileInput');
    if(fileInput.files.length === 0) return;
    const file = fileInput.files[0];
    const reader = new FileReader();
    reader.onload = function(e) {
        document.getElementById('textInput').value = e.target.result;
        resetHighlights();
        highlightAll(); // автопідсвітка після завантаження
    };
    reader.readAsText(file);
}

function escapeHTML(str) {
    return str.replace(/&/g, "&")
              .replace(/</g, "<")
              .replace(/>/g, ">")
              .replace(/"/g, """)
              .replace(/'/g, "'");
}

// Автопідсвітка при зміні тексту
document.getElementById('textInput').addEventListener('input', highlightAll);

// Виклик при завантаженні сторінки
highlightAll();


Назад Зміст