Skip to content

Commit 84bb130

Browse files
authored
Don't rely on word boundary in mentions (#45)
They don't work reliably with unicode characters even with u or v flags. Also, usernames can end on non-word characters.
1 parent 54cd73d commit 84bb130

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/components/Chat.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ const useStyles = makeStyles((theme) => ({
6565
},
6666
}));
6767

68-
const makeUserRE = (username) => {
68+
const makeMentionRE = (username) => {
6969
username = username.replace(/[\\^$.*+?()[\]{}|]/g, "\\$&");
7070
username = username.replace(/^anonymous /i, "($&)?");
71-
return new RegExp(`@(all|${username})\\b`, "i");
71+
return new RegExp(`@(all|${username})(\\W|$)`, "iu");
7272
};
7373

7474
/** A chat sidebar element, opens lobby chat when the `gameId` prop is not set. */
@@ -106,10 +106,10 @@ function Chat({
106106
[databasePath, messageLimit]
107107
);
108108
const messages = useFirebaseQuery(messagesQuery);
109-
const userRE = useMemo(() => makeUserRE(user.name), [user.name]);
109+
const mentionRE = useMemo(() => makeMentionRE(user.name), [user.name]);
110110

111111
const addMentioned = (cls, message) => {
112-
return userRE.test(message) ? `${cls} ${classes.mentioned}` : cls;
112+
return mentionRE.test(message) ? `${cls} ${classes.mentioned}` : cls;
113113
};
114114

115115
function handleSubmit(event) {

0 commit comments

Comments
 (0)