Skip to content

Commit 100a5fc

Browse files
authored
fix: curly brace escaping in regular expression (#570)
### Description Of Changes The modification in the code was to escape the curly braces ({}) by replacing them with \{ and \}. This ensures that the curly braces are treated as literal characters in the regular expression, addressing the issue caused by their metacharacter usage. [UIKIT-4027](https://sendbird.atlassian.net/browse/UIKIT-4027)
1 parent 4035f3f commit 100a5fc

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/modules/Message/utils/tokens/__tests__/tokenizeUtils.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('getUserMentionRegex', () => {
1616
] as User[];
1717
const templatePrefix = '@';
1818
const result = getUserMentionRegex(mentionedUsers, templatePrefix);
19-
expect(result).toEqual(/(@{1}|@{2})/g);
19+
expect(result).toEqual(/(@\{1\}|@\{2\})/g);
2020
});
2121

2222
it('should return a correct regex pattern; userId includes some patterns need to be escaped', () => {
@@ -26,7 +26,7 @@ describe('getUserMentionRegex', () => {
2626
] as User[];
2727
const templatePrefix = '@';
2828
const result = getUserMentionRegex(mentionedUsers, templatePrefix);
29-
expect(result).toEqual(/(@{1\*}|@{2\+})/g);
29+
expect(result).toEqual(/(@\{1\*\}|@\{2\+\})/g);
3030
});
3131
});
3232

src/modules/Message/utils/tokens/tokenize.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ export function getUserMentionRegex(mentionedUsers: User[], templatePrefix_: str
1111
// If user.id includes these patterns, need to convert it into an escaped one
1212
/([.*+?^${}()|[\]\\])/g,
1313
'\\$1');
14-
return `${templatePrefix}{${userId}}`;
14+
/**
15+
* //{ And //} are also for escaping
16+
* because curly braces `{}` are metacharacters in regular expressions used to specify repetition
17+
*/
18+
return `${templatePrefix}\\{${userId}\\}`;
1519
}).join('|')})`, 'g');
1620
}
1721

1822
export function identifyMentions({
1923
tokens,
2024
mentionedUsers = [],
2125
templatePrefix = USER_MENTION_PREFIX,
22-
}: IdentifyMentionsType): (MentionToken|UndeterminedToken)[] {
26+
}: IdentifyMentionsType): (MentionToken | UndeterminedToken)[] {
2327
if (!mentionedUsers?.length) {
2428
return tokens;
2529
}

0 commit comments

Comments
 (0)