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.
This commit is contained in:
Jordan Brough 2022-12-05 18:02:43 -06:00 committed by GitHub
parent 8004f67efe
commit b9fd1d88ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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'),
'<span class="searchkey--highlight">$1</span>'
);
},
// from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
},
},
};
</script>