fix: code examples searc componenth toescape special characters in regex formattion
This commit is contained in:
@@ -22,7 +22,8 @@ export const CodeExamples = () => {
|
|||||||
const isSearching = !!search
|
const isSearching = !!search
|
||||||
let searchResults: typeof productCodeExamples = []
|
let searchResults: typeof productCodeExamples = []
|
||||||
if (isSearching) {
|
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) => {
|
searchResults = productCodeExamples.filter((example) => {
|
||||||
const searchableStr = `${example.tags.join(' ')} ${example.title} ${example.description}`
|
const searchableStr = `${example.tags.join(' ')} ${example.title} ${example.description}`
|
||||||
return matchReg.test(searchableStr)
|
return matchReg.test(searchableStr)
|
||||||
|
|||||||
Reference in New Issue
Block a user