From b9fd1d88eae113fbedcb41478833e9c2b2b66977 Mon Sep 17 00:00:00 2001 From: Jordan Brough Date: Mon, 5 Dec 2022 18:02:43 -0600 Subject: [PATCH] Escape search term before building regular expression (#5994) When doing a conversation search, if the search term includes any regular expression characters and the search returns results, then this function would throw an exception. For example, if a conversation includes the text "+15555550111" and you search for "+15555550111" then you get an exception like: > Invalid regular expression: /(+15555550111)/: Nothing to repeat Because the "+" is not escaped. --- .../dashboard/conversation/search/SearchMessageItem.vue | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/javascript/dashboard/routes/dashboard/conversation/search/SearchMessageItem.vue b/app/javascript/dashboard/routes/dashboard/conversation/search/SearchMessageItem.vue index ee66d2646..172c8f250 100644 --- a/app/javascript/dashboard/routes/dashboard/conversation/search/SearchMessageItem.vue +++ b/app/javascript/dashboard/routes/dashboard/conversation/search/SearchMessageItem.vue @@ -64,11 +64,16 @@ export default { methods: { prepareContent(content = '') { const plainTextContent = this.getPlainText(content); + const escapedSearchTerm = this.escapeRegExp(this.searchTerm); return plainTextContent.replace( - new RegExp(`(${this.searchTerm})`, 'ig'), + new RegExp(`(${escapedSearchTerm})`, 'ig'), '$1' ); }, + // from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping + escapeRegExp(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + }, }, };