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:
parent
8004f67efe
commit
b9fd1d88ea
1 changed files with 6 additions and 1 deletions
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue