From a5200e3f753fc907677c2136fb2db8636863cab2 Mon Sep 17 00:00:00 2001 From: Hailey Date: Tue, 23 Apr 2024 16:56:22 -0700 Subject: [PATCH] improve regex --- src/view/com/composer/text-input/text-input-util.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/view/com/composer/text-input/text-input-util.ts b/src/view/com/composer/text-input/text-input-util.ts index 8119e429cf..a37456bcdb 100644 --- a/src/view/com/composer/text-input/text-input-util.ts +++ b/src/view/com/composer/text-input/text-input-util.ts @@ -47,13 +47,17 @@ function isValidUrlAndDomain(value: string) { ) } +function escapeRegex(literal: string) { + return literal.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&') +} + export function findIndexInText(term: string, text: string) { // This should find patterns like: // HELLO SENTENCE http://google.com/ HELLO // HELLO SENTENCE http://google.com HELLO // http://google.com/ HELLO. // http://google.com/. - const pattern = new RegExp(`\\b(${term})(?![/w])`, 'i') + const pattern = new RegExp(`\\b(${escapeRegex(term)})(?!w)`, 'i') const match = pattern.exec(text) return match ? match.index : -1 }