alert_words: Replace escape_user_regex with _.escapeRegExp.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2020-07-29 13:18:53 -07:00 committed by Tim Abbott
parent 8e79e0e63b
commit 4a26bde4bd
1 changed files with 3 additions and 7 deletions

View File

@ -1,3 +1,5 @@
const _ = require("lodash");
// For simplicity, we use a list for our internal // For simplicity, we use a list for our internal
// data, since that matches what the server sends us. // data, since that matches what the server sends us.
let my_alert_words = []; let my_alert_words = [];
@ -19,12 +21,6 @@ exports.has_alert_word = function (word) {
return my_alert_words.includes(word); return my_alert_words.includes(word);
}; };
// escape_user_regex taken from jquery-ui/autocomplete.js,
// licensed under MIT license.
function escape_user_regex(value) {
return value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
exports.process_message = function (message) { exports.process_message = function (message) {
// Parsing for alert words is expensive, so we rely on the host // Parsing for alert words is expensive, so we rely on the host
// to tell us there any alert words to even look for. // to tell us there any alert words to even look for.
@ -33,7 +29,7 @@ exports.process_message = function (message) {
} }
for (const word of my_alert_words) { for (const word of my_alert_words) {
const clean = escape_user_regex(word); const clean = _.escapeRegExp(word);
const before_punctuation = "\\s|^|>|[\\(\\\".,';\\[]"; const before_punctuation = "\\s|^|>|[\\(\\\".,';\\[]";
const after_punctuation = "\\s|$|<|[\\)\\\"\\?!:.,';\\]!]"; const after_punctuation = "\\s|$|<|[\\)\\\"\\?!:.,';\\]!]";