1
0
mirror of synced 2025-12-22 11:26:57 -05:00

fix: code examples searc componenth toescape special characters in regex formattion

This commit is contained in:
Ritesh Patil
2022-04-03 19:26:19 +00:00
committed by GitHub
parent 5c73576c8c
commit a739577639

View File

@@ -22,7 +22,8 @@ export const CodeExamples = () => {
const isSearching = !!search
let searchResults: typeof productCodeExamples = []
if (isSearching) {
const matchReg = new RegExp(search, 'i')
// The following replace method escapes special characters in regular expression creation.
const matchReg = new RegExp(search.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'), 'i')
searchResults = productCodeExamples.filter((example) => {
const searchableStr = `${example.tags.join(' ')} ${example.title} ${example.description}`
return matchReg.test(searchableStr)