Skip to content

Commit 66fc814

Browse files
committed
fix: safely escape strings passed to RegExp in InputSearch
1 parent 0cb0ae6 commit 66fc814

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

packages/kit/src/input-search/InputSearchListItem.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,15 @@ export const ListItem = ({ item, state }: ListItemProps) => {
8585
);
8686

8787
let highlighted;
88+
89+
const escape = (string) => {
90+
return string.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&");
91+
};
92+
8893
if (typeof item.rendered === "string") {
89-
highlighted = item.rendered.replace(
90-
new RegExp((state as ComboBoxState<object>).inputValue, "gi"),
91-
(match) => (match ? `<mark>${match}</mark>` : "")
94+
const val = escape((state as ComboBoxState<object>).inputValue);
95+
highlighted = item.rendered.replace(new RegExp(val, "gi"), (match) =>
96+
match ? `<mark>${match}</mark>` : ""
9297
);
9398
}
9499

0 commit comments

Comments
 (0)