diff --git a/src/utils.test.ts b/src/utils.test.ts index 7031b7ff..6f9cbdf9 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -21,4 +21,11 @@ describe("highlightSubString function ", () => { match: false, }); }); + + it("handles special substrings for regex matching", () => { + expect(highlightSubString("somestring\\", "\\")).toEqual({ + text: "somestring\\", + match: true, + }); + }); }); diff --git a/src/utils.ts b/src/utils.ts index cca6daaf..0bb1e20a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -19,7 +19,9 @@ export const highlightSubString = ( match: false, }; } - const caseInsensitiveRegex = new RegExp(subString, "gi"); + + const escapedSubstring = subString.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + const caseInsensitiveRegex = new RegExp(escapedSubstring, "gi"); const newStr = str.replace( caseInsensitiveRegex, (match) => `${match}`,