diff --git a/surfsense_web/components/chat-comments/comment-composer/comment-composer.tsx b/surfsense_web/components/chat-comments/comment-composer/comment-composer.tsx index 3e9b4504f..bee3f2da6 100644 --- a/surfsense_web/components/chat-comments/comment-composer/comment-composer.tsx +++ b/surfsense_web/components/chat-comments/comment-composer/comment-composer.tsx @@ -15,13 +15,17 @@ function convertDisplayToData(displayContent: string, mentions: InsertedMention[ const sortedMentions = [...mentions].sort((a, b) => b.displayName.length - a.displayName.length); - for (const mention of sortedMentions) { - const displayPattern = new RegExp( + const mentionPatterns = sortedMentions.map((mention) => ({ + pattern: new RegExp( `@${escapeRegExp(mention.displayName)}(?=\\s|$|[.,!?;:])`, "g" - ); - const dataFormat = `@[${mention.id}]`; - result = result.replace(displayPattern, dataFormat); + ), + dataFormat: `@[${mention.id}]`, + })); + + for (const { pattern, dataFormat } of mentionPatterns) { + pattern.lastIndex = 0; // reset global regex state + result = result.replace(pattern, dataFormat); } return result;