@@ -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