From 0260a968f0d927b9cdc4884dd5d71df571a0f07b Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Tue, 4 Jun 2013 17:33:18 +0100 Subject: [PATCH] Escape special characters before embedding them in a regular expression. Fixes #279 --- client/assets/src/helpers/utils.js | 24 +++++------------------- client/assets/src/views/panel.js | 4 ++-- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/client/assets/src/helpers/utils.js b/client/assets/src/helpers/utils.js index 75875ee..5944db7 100644 --- a/client/assets/src/helpers/utils.js +++ b/client/assets/src/helpers/utils.js @@ -95,10 +95,6 @@ function secondsToTime(secs) { } - - - - /* Command input Alias + re-writing */ function InputPreProcessor () { this.recursive_depth = 3; @@ -199,15 +195,6 @@ function InputPreProcessor () { } - - - - - - - - - /** * Convert HSL to RGB formatted colour */ @@ -251,9 +238,6 @@ function hsl2rgb(h, s, l) { } - - - /** * Formats a message. Adds bold, underline and colouring * @param {String} msg The message to format @@ -394,9 +378,11 @@ function formatIRCMsg (msg) { } - - function formatDate (d) { d = d || new Date(); return d.toLocaleDateString() + ', ' + d.getHours().toString() + ':' + d.getMinutes().toString() + ':' + d.getSeconds().toString(); -} \ No newline at end of file +} + +function escapeRegex (str) { + return str.replace(/[\[\\\^\$\.\|\?\*\+\(\)]/g, '\\$&'); +} diff --git a/client/assets/src/views/panel.js b/client/assets/src/views/panel.js index 5ecba80..0ed3480 100644 --- a/client/assets/src/views/panel.js +++ b/client/assets/src/views/panel.js @@ -48,7 +48,7 @@ _kiwi.view.Panel = Backbone.View.extend({ nick_colour_hex, nick_hex, is_highlight, msg_css_classes = ''; // Nick highlight detecting - if ((new RegExp('\\b' + _kiwi.app.connections.active_connection.get('nick') + '\\b', 'i')).test(msg.msg)) { + if ((new RegExp('(^|\\W)(' + escapeRegex(_kiwi.app.connections.active_connection.get('nick')) + ')(\\W|$)', 'i')).test(msg.msg)) { is_highlight = true; msg_css_classes += ' highlight'; } @@ -57,7 +57,7 @@ _kiwi.view.Panel = Backbone.View.extend({ msg.msg = $('
').text(msg.msg).html(); // Make the channels clickable - re = new RegExp('(?:^|\\s)([' + _kiwi.gateway.get('channel_prefix') + '][^ ,.\\007]+)', 'g'); + re = new RegExp('(?:^|\\s)([' + escapeRegex(_kiwi.gateway.get('channel_prefix')) + '][^ ,.\\007]+)', 'g'); msg.msg = msg.msg.replace(re, function (match) { return '' + match + ''; }); -- 2.25.1