diff --git a/client/js/options.js b/client/js/options.js index 20f94289..798da506 100644 --- a/client/js/options.js +++ b/client/js/options.js @@ -1,6 +1,7 @@ "use strict"; const $ = require("jquery"); +const escapeRegExp = require("lodash/escapeRegExp"); const settings = $("#settings"); const userStyles = $("#user-specified-css"); const storage = require("./localStorage"); @@ -98,6 +99,15 @@ settings.on("change", "input, select, textarea", function() { // otherwise, users get notifications for everything return h !== ""; }); + // Construct regex with wordboundary for every highlight item + const highlightsTokens = options.highlights.map(function(h) { + return escapeRegExp(h); + }); + if (highlightsTokens && highlightsTokens.length) { + module.exports.highlightsRE = new RegExp("\\b(?:" + highlightsTokens.join("|") + ")\\b", "i"); + } else { + module.exports.highlightsRE = null; + } } else if (name === "showSeconds") { chat.find(".msg > .time").each(function() { $(this).text(tz($(this).parent().data("time"))); diff --git a/client/js/render.js b/client/js/render.js index 9c74a25d..03b73158 100644 --- a/client/js/render.js +++ b/client/js/render.js @@ -73,9 +73,11 @@ function buildChatMessage(data) { const chan = chat.find(target); let template = "msg"; - if (!data.msg.highlight && !data.msg.self && (type === "message" || type === "notice") && options.highlights.some(function(h) { - return data.msg.text.toLocaleLowerCase().indexOf(h.toLocaleLowerCase()) > -1; - })) { + // See if any of the custom highlight regexes match + if (!data.msg.highlight && !data.msg.self + && options.highlightsRE + && (type === "message" || type === "notice") + && options.highlightsRE.exec(data.msg.text)) { data.msg.highlight = true; }