diff --git a/tgui/packages/tgui-panel/chat/renderer.js b/tgui/packages/tgui-panel/chat/renderer.js index f7ce9277cf68..fe175ee6d94e 100644 --- a/tgui/packages/tgui-panel/chat/renderer.js +++ b/tgui/packages/tgui-panel/chat/renderer.js @@ -193,6 +193,7 @@ class ChatRenderer { const matchWord = setting.matchWord; const matchCase = setting.matchCase; const allowedRegex = /^[a-z0-9_\-$/^[\s\]\\]+$/gi; + const regexEscapeCharacters = /[!#$%^&*)(+=.<>{}[\]:;'"|~`_\-\\/]/g; const lines = String(text) .split(',') .map((str) => str.trim()) @@ -228,19 +229,27 @@ class ChatRenderer { if (!highlightWords) { highlightWords = []; } + // We're not going to let regex characters fuck up our RegEx operation. + line = line.replace(regexEscapeCharacters, '\\$&'); + highlightWords.push(line); } } - const regexStr = regexExpressions.join('|'); - const flags = 'g' + (matchCase ? '' : 'i'); - // setting regex overrides matchword - if (regexStr) { - highlightRegex = new RegExp('(' + regexStr + ')', flags); - } else { - const pattern = `${matchWord ? '\\b' : ''}(${lines.join('|')})${ - matchWord ? '\\b' : '' - }`; - highlightRegex = new RegExp(pattern, flags); + // We wrap this in a try-catch to ensure that broken regex doesn't break + // the entire chat. + try { + // setting regex overrides matchword + if (regexStr) { + highlightRegex = new RegExp('(' + regexStr + ')', flags); + } else { + const pattern = `${matchWord ? '\\b' : ''}(${highlightWords.join( + '|' + )})${matchWord ? '\\b' : ''}`; + highlightRegex = new RegExp(pattern, flags); + } + } catch { + // We just reset it if it's invalid. + highlightRegex = null; } // Lazy init if (!this.highlightParsers) {