Skip to content

Commit e337abe

Browse files
author
Raffael Herrmann
committed
feat: add regex support to search functionality
- Update search placeholder to indicate regex support - Modify search functions to use regex patterns directly - Add error handling for invalid regex patterns - Fall back to literal string search when regex is invalid
1 parent 765a50b commit e337abe

2 files changed

Lines changed: 25 additions & 7 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..." 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="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">
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: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,11 @@ document.addEventListener('DOMContentLoaded', () => {
222222
let activeTabIndex = 0;
223223

224224
// Check name
225-
if (snippet.name.toLowerCase().includes(query)) {
225+
if (matchesQuery(snippet.name, query)) {
226226
hasMatch = true;
227227
}
228228
// Check description
229-
if (snippet.description?.toLowerCase().includes(query)) {
229+
if (snippet.description && matchesQuery(snippet.description, query)) {
230230
hasMatch = true;
231231
}
232232

@@ -295,14 +295,32 @@ document.addEventListener('DOMContentLoaded', () => {
295295
}
296296
});
297297

298+
// Function to check if text matches query (supports regex)
299+
function matchesQuery(text, query) {
300+
if (!query.trim()) return false;
301+
try {
302+
const regex = new RegExp(query, 'i'); // case-insensitive
303+
return regex.test(text);
304+
} catch (error) {
305+
// If regex is invalid, fall back to literal string search
306+
return text.toLowerCase().includes(query.toLowerCase());
307+
}
308+
}
309+
298310
// Function to highlight search matches in text
299311
function highlightSearchMatches(text, query) {
300312
if (!query.trim()) return text;
301313

302-
// Escape special regex characters in query
303-
const escapedQuery = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
304-
const regex = new RegExp(`(${escapedQuery})`, 'gi');
305-
return text.replace(regex, '<mark class="search-highlight">$1</mark>');
314+
try {
315+
// 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>');
318+
} catch (error) {
319+
// If regex is invalid, escape special characters and search as literal string
320+
const escapedQuery = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
321+
const regex = new RegExp(`(${escapedQuery})`, 'gi');
322+
return text.replace(regex, '<mark class="search-highlight">$1</mark>');
323+
}
306324
}
307325

308326
function renderSnippets(snippets, clear = false) {

0 commit comments

Comments
 (0)