Don't require boundaries for non-word characters

This fixes an issue where highlight regular expression like :-?\)
couldn't match a smiley, unless it happened to have a word character
on both of its sides.
This commit is contained in:
Konrad Borowski
2015-09-03 15:46:22 +02:00
parent a02a93123a
commit 1b593da4e5
2 changed files with 11 additions and 4 deletions

View File

@@ -253,7 +253,7 @@
var highlights = Tools.prefs('highlights') || [];
if (!app.highlightRegExp) {
try {
app.highlightRegExp = new RegExp('\\b(' + highlights.join('|') + ')\\b', 'i');
this.updateHighlightRegExp(highlights);
} catch (e) {
// If the expression above is not a regexp, we'll get here.
// Don't throw an exception because that would prevent the chat
@@ -267,6 +267,13 @@
}
return ((highlights.length > 0) && app.highlightRegExp.test(message));
},
updateHighlightRegExp: function (highlights) {
// Enforce boundary for match sides, if a letter on match side is
// a word character. For example, regular expression "a" matches
// "a", but not "abc", while regular expression "!" matches
// "!" and "!abc".
app.highlightRegExp = new RegExp('(?:\\b|(?!\\w))(?:' + highlights.join('|') + ')(?:\\b|\\B(?!\\w))', 'i');
},
// chat history
@@ -711,7 +718,7 @@
highlights = highlights.concat(targets.slice(1));
this.add("Now highlighting on: " + highlights.join(', '));
// We update the regex
app.highlightRegExp = new RegExp('\\b(' + highlights.join('|') + ')\\b', 'i');
this.updateHighlightRegExp(highlights);
break;
case 'delete':
var newHls = [];
@@ -723,7 +730,7 @@
highlights = newHls;
this.add("Now highlighting on: " + highlights.join(', '));
// We update the regex
app.highlightRegExp = new RegExp('\\b(' + highlights.join('|') + ')\\b', 'i');
this.updateHighlightRegExp(highlights);
break;
default:
// Wrong command

View File

@@ -185,7 +185,7 @@
if (!self.get('named')) {
self.nameRegExp = null;
} else {
self.nameRegExp = new RegExp('\\b' + Tools.escapeRegExp(self.get('name')) + '\\b', 'i');
self.nameRegExp = new RegExp('(?:\\b|(?!\\w))' + Tools.escapeRegExp(self.get('name')) + '(?:\\b|\\B(?!\\w))', 'i');
}
});
},