improve regex, fixes replacements of usernames with same trunk

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2016-10-17 18:19:51 +02:00
parent 0e926efc9b
commit b21ce6ee5e
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
1 changed files with 12 additions and 1 deletions

View File

@ -256,9 +256,20 @@
for(var i in mentions) {
var mention = '@' + mentions[i].mentionId;
// escape possible regex characters in the name
mention = mention.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
var displayName = '<b>'+ _.escape(mentions[i].mentionDisplayName)+'</b>';
message = message.replace(new RegExp(mention, 'g'), displayName);
// replace every mention either at the start of the input or after a whitespace
// followed by a non-word character.
message = message.replace(new RegExp("(^|\\s)(" + mention + ")(\\b|?![_-@.])", 'g'),
function(match, p1) {
// to get number of whitespaces (0 vs 1) right
return p1+displayName;
}
);
}
return message;