Escape special characters before embedding them in a regular expression.
authorJack Allnutt <m2ys4u@gmail.com>
Tue, 4 Jun 2013 16:33:18 +0000 (17:33 +0100)
committerJack Allnutt <m2ys4u@gmail.com>
Tue, 4 Jun 2013 16:37:25 +0000 (17:37 +0100)
Fixes #279

client/assets/src/helpers/utils.js
client/assets/src/views/panel.js

index 75875eef2be0060dbff40c38ea030ef546d20bb2..5944db7dc09636c3fe83d62956926845ad5e3664 100644 (file)
@@ -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, '\\$&');
+}
index 5ecba801d88820826f4313ae5ee54b335d170150..0ed348017eaf9767bc4a558db5f8ea9c41edddaa 100644 (file)
@@ -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 =  $('<div />').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 '<a class="chan" data-channel="' + match.trim() + '">' + match + '</a>';
         });