Skip to content

Commit 53106af

Browse files
author
Raffael Herrmann
committed
feat: make regex search case sensitive and limit highlights to 3 matches
- Remove case-insensitive flag from regex patterns - Remove query.toLowerCase() processing - Limit search highlighting to first 3 matches per snippet - Update fallback literal search to be case sensitive
1 parent e337abe commit 53106af

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ <h1 class="text-2xl font-bold">massCode-Web</h1>
3333
🌓
3434
</button>
3535
<div class="relative">
36-
<input type="text" id="search" placeholder="Search snippets (regex supported)..." class="w-64 px-3 py-2 pr-8 border border-gray-200 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 text-gray-900 dark:bg-gray-700 dark:border-gray-600 dark:text-white dark:placeholder-gray-400 transition-colors">
36+
<input type="text" id="search" placeholder="Enter RegEx..." class="w-64 px-3 py-2 pr-8 border border-gray-200 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 text-gray-900 dark:bg-gray-700 dark:border-gray-600 dark:text-white dark:placeholder-gray-400 transition-colors">
3737
<button id="clear-search" class="absolute right-2 top-1/2 transform -translate-y-1/2 text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200 transition-colors" onclick="clearSearch()" style="display: none;">×</button>
3838
</div>
3939
</div>

public/js/app.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ document.addEventListener('DOMContentLoaded', () => {
202202
// Search functionality
203203
const clearSearchButton = document.getElementById('clear-search');
204204
searchInput.addEventListener('input', (e) => {
205-
const query = e.target.value.toLowerCase();
205+
const query = e.target.value;
206206
const activeSnippets = dbData.snippets.filter(s => !s.isDeleted);
207207

208208
if (query.trim() === '') {
@@ -299,11 +299,11 @@ document.addEventListener('DOMContentLoaded', () => {
299299
function matchesQuery(text, query) {
300300
if (!query.trim()) return false;
301301
try {
302-
const regex = new RegExp(query, 'i'); // case-insensitive
302+
const regex = new RegExp(query); // case-sensitive
303303
return regex.test(text);
304304
} catch (error) {
305305
// If regex is invalid, fall back to literal string search
306-
return text.toLowerCase().includes(query.toLowerCase());
306+
return text.includes(query);
307307
}
308308
}
309309

@@ -313,13 +313,27 @@ document.addEventListener('DOMContentLoaded', () => {
313313

314314
try {
315315
// Try to use the query as a regex pattern
316-
const regex = new RegExp(`(${query})`, 'gi');
317-
return text.replace(regex, '<mark class="search-highlight">$1</mark>');
316+
const regex = new RegExp(`(${query})`, 'g'); // case-sensitive, global
317+
let matchCount = 0;
318+
return text.replace(regex, (match, group1) => {
319+
matchCount++;
320+
if (matchCount <= 3) {
321+
return `<mark class="search-highlight">${group1}</mark>`;
322+
}
323+
return group1; // Return without highlighting for matches beyond 3
324+
});
318325
} catch (error) {
319326
// If regex is invalid, escape special characters and search as literal string
320327
const escapedQuery = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
321-
const regex = new RegExp(`(${escapedQuery})`, 'gi');
322-
return text.replace(regex, '<mark class="search-highlight">$1</mark>');
328+
const regex = new RegExp(`(${escapedQuery})`, 'g');
329+
let matchCount = 0;
330+
return text.replace(regex, (match, group1) => {
331+
matchCount++;
332+
if (matchCount <= 3) {
333+
return `<mark class="search-highlight">${group1}</mark>`;
334+
}
335+
return group1; // Return without highlighting for matches beyond 3
336+
});
323337
}
324338
}
325339

0 commit comments

Comments
 (0)