HTML and JavaScript Solution to Find and Extract Text
Here's a complete solution that allows users to open a file, find text after ":" in each line, and extract content between parentheses from lines before the last match:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text Extractor</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
textarea {
width: 100%;
height: 200px;
margin: 10px 0;
}
button {
padding: 8px 16px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.results {
margin-top: 20px;
padding: 10px;
border: 1px solid #ddd;
background-color: #f9f9f9;
}
</style>
</head>
<body>
<h1>Text Extractor</h1>
<input type="file" id="fileInput" accept=".txt,.csv,.log,.json,.xml">
<button onclick="processFile()">Process File</button>
<h3>File Content:</h3>
<textarea id="fileContent" readonly></textarea>
<h3>Results:</h3>
<div class="results">
<h4>Text after ':' in each line:</h4>
<div id="afterColonResults"></div>
<h4>Content between parentheses from lines before last match:</h4>
<div id="parenthesesResults"></div>
</div>
<script>
function processFile() {
const fileInput = document.getElementById('fileInput');
const fileContent = document.getElementById('fileContent');
const afterColonResults = document.getElementById('afterColonResults');
const parenthesesResults = document.getElementById('parenthesesResults');
const file = fileInput.files[0];
if (!file) {
alert('Please select a file first.');
return;
}
const reader = new FileReader();
reader.onload = function(e) {
const content = e.target.result;
fileContent.value = content;
// Process the content
const lines = content.split('\n');
let afterColonTexts = [];
let parenthesesContents = [];
let lastMatchIndex = -1;
// First pass: find text after ':' and note the last match position
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
const colonIndex = line.indexOf(':');
if (colonIndex !== -1) {
const textAfterColon = line.substring(colonIndex + 1).trim();
afterColonTexts.push(textAfterColon);
lastMatchIndex = i;
}
}
// Second pass: find content between parentheses in lines before last match
if (lastMatchIndex > 0) {
for (let i = 0; i < lastMatchIndex; i++) {
const line = lines[i];
const regex = /\((.*?)\)/g;
let match;
while ((match = regex.exec(line)) !== null) {
parenthesesContents.push(match[1]);
}
}
}
// Display results
afterColonResults.innerHTML = afterColonTexts.length > 0
? '<ul>' + afterColonTexts.map(text => `<li>${text}</li>`).join('') + '</ul>'
: '<p>No text found after ":" in any line.</p>';
parenthesesResults.innerHTML = parenthesesContents.length > 0
? '<ul>' + parenthesesContents.map(text => `<li>${text}</li>`).join('') + '</ul>'
: '<p>No content found between parentheses in lines before the last match.</p>';
};
reader.readAsText(file);
}
</script>
</body>
</html>
How This Works:
-
File Selection: Users can select a text file using the file input.
-
Processing:
- The file is read and split into lines
- First pass finds all text after ":" in each line and records the position of the last match
- Second pass finds all text between parentheses in lines before the last match
-
Results Display:
- Shows all text found after ":" in each line
- Shows all content found between parentheses in lines before the last match
-
Features:
- Handles multiple matches per line
- Preserves the original file content for reference
- Clear visual separation of different result types
You can save this code as an HTML file and open it in any modern browser to use the tool.