Okay, here's an HTML and JavaScript solution that allows you to select a local file, read its content, find the last line containing a colon (:), extract the text after that colon, and then highlight all lines before that last found line if they contain both opening ( and closing ) parentheses.
How it works:
-
HTML Structure:
- An
<input type="file"> element to let the user select a file.
- A
<div> to display the extracted text.
- A
<pre> element to display the processed file content, preserving whitespace and line breaks.
-
JavaScript Logic:
- It listens for changes on the file input (
'change' event).
- When a file is selected, it uses
FileReader to read the file's content as text.
- It splits the content into individual lines.
- It iterates backwards through the lines to efficiently find the
last line containing a colon.
- Once the last colon line is found, it extracts the substring after the colon.
- It then iterates forwards through all lines.
- If a line is before the
lastColonLineIndex and contains both ( and ), it wraps the line in a <span> with a special CSS class for highlighting.
- The
lastColonLineIndex itself is also highlighted (optional, for clarity).
- Finally, it updates the display areas with the extracted text and the (potentially) highlighted file content.
HTML & JavaScript Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Processor</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f8f8f8;
color: #333;
}
h1, h2 {
color: #0056b3;
}
input[type="file"] {
margin-bottom: 15px;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
cursor: pointer;
}
#extractedTextDisplay, #fileContentDisplay {
margin-top: 20px;
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: #fff;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
#extractedTextDisplay {
font-weight: bold;
color: #28a745; /* Green for success/found text */
}
#fileContentDisplay {
white-space: pre-wrap; /* Preserve whitespace and wrap long lines */
word-wrap: break-word; /* For older browsers */
font-family: 'Courier New', Courier, monospace; /* Monospace for code/text */
max-height: 500px; /* Limit height for scroll */
overflow-y: auto; /* Enable scrolling */
}
/* CSS for highlighting */
.highlight-parentheses {
background-color: #ffeb3b; /* Yellow */
font-weight: bold;
padding: 1px 3px;
border-radius: 3px;
}
.highlight-colon-line {
background-color: #bbdefb; /* Light Blue */
font-style: italic;
padding: 1px 3px;
border-radius: 3px;
}
</style>
</head>
<body>
<h1>File Text Processor</h1>
<p>Select a text file to process:</p>
<input type="file" id="fileInput" accept=".txt, .log, .csv, .js, .html">
<h2>Extracted Text (after last colon):</h2>
<div id="extractedTextDisplay">
No file selected or no colon found yet.
</div>
<h2>Processed File Content:</h2>
<pre id="fileContentDisplay">
File content will appear here. Lines before the last line containing ':' that also have '(' and ')' will be highlighted.
</pre>
<script>
document.getElementById('fileInput').addEventListener('change', function(event) {
const file = event.target.files[0];
if (!file) {
document.getElementById('extractedTextDisplay').textContent = "No file selected.";
document.getElementById('fileContentDisplay').textContent = "File content will appear here.";
return;
}
const reader = new FileReader();
reader.onload = function(e) {
const fileContent = e.target.result;
const lines = fileContent.split(/\r?\n/); // Split by new line, handling Windows/Unix formats
let lastColonLineIndex = -1;
let extractedText = "No line with ':' found.";
// 1. Find the last line with a colon (iterate backwards)
for (let i = lines.length - 1; i >= 0; i--) {
if (lines[i].includes(':')) {
lastColonLineIndex = i;
// Extract text after the first colon on this line
extractedText = lines[i].substring(lines[i].indexOf(':') + 1).trim();
break; // Found the last one, stop
}
}
// Update the extracted text display
document.getElementById('extractedTextDisplay').textContent = extractedText;
// 2. Process lines for display and highlighting
const processedLines = [];
for (let i = 0; i < lines.length; i++) {
let currentLine = lines[i];
if (i < lastColonLineIndex) {
// This line is *before* the last line with a colon
if (currentLine.includes('(') && currentLine.includes(')')) {
// Line contains both parentheses, highlight it
processedLines.push(`<span class="highlight-parentheses">${escapeHTML(currentLine)}</span>`);
} else {
processedLines.push(escapeHTML(currentLine));
}
} else if (i === lastColonLineIndex) {
// This is the last line with a colon (optional highlight)
processedLines.push(`<span class="highlight-colon-line">${escapeHTML(currentLine)}</span>`);
} else {
// This line is after the last line with a colon, or no colon was found
processedLines.push(escapeHTML(currentLine));
}
}
// Display the processed file content
document.getElementById('fileContentDisplay').innerHTML = processedLines.join('\n');
};
reader.onerror = function() {
document.getElementById('extractedTextDisplay').textContent = "Error reading file.";
document.getElementById('fileContentDisplay').textContent = "Could not read file content.";
};
// Read the file as text
reader.readAsText(file);
});
// Helper function to escape HTML characters to prevent XSS and display raw text correctly
function escapeHTML(str) {
const div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML;
}
</script>
</body>
</html>
To Use This Code:
- Save the entire code block as an HTML file (e.g.,
file_processor.html).
- Open this HTML file in your web browser.
- Click the "Choose File" button.
- Select a text-based file from your computer (e.g.,
.txt, .log, .js, etc.).
Example File Content to Test With (e.g., my_data.txt):
This is a test line.
Another line with (some) content.
Key1: Value for Key1
(Important) configuration (data).
A simple line.
Status: Running (version 1.2) check.
Data point: This is the last value with a colon.
(Skipped) This line is after the last colon.
Final line.
Expected Output for my_data.txt:
- Extracted Text:
This is the last value with a colon.
- Processed File Content:
This is a test line. (normal)
Another line with (some) content. (highlighted yellow)
Key1: Value for Key1 (normal)
(Important) configuration (data). (highlighted yellow)
A simple line. (normal)
Status: Running (version 1.2) check. (normal)
Data point: This is the last value with a colon. (highlighted light blue - this is the "last found colon line")
(Skipped) This line is after the last colon. (normal - because it's after the last colon line, even though it has parentheses)
Final line. (normal)
This solution fully addresses your requirements.