fix(regexp): escape inputs

This commit is contained in:
Matteo Pagliazzi
2020-04-28 16:00:17 +02:00
parent 783b8995b8
commit b947c714f0
3 changed files with 13 additions and 3 deletions

View File

@@ -1,3 +1,4 @@
import escapeRegExp from 'lodash/escapeRegExp';
export function removePunctuationFromString (str) {
return str.replace(/[.,/#!@$%^&;:{}=\-_`~()]/g, ' ');
@@ -5,7 +6,10 @@ export function removePunctuationFromString (str) {
export function getMatchesByWordArray (str, wordsToMatch) {
const matchedWords = [];
const wordRegexs = wordsToMatch.map(word => new RegExp(`\\b([^a-z]+)?${word}([^a-z]+)?\\b`, 'i'));
const wordRegexs = wordsToMatch.map(word => {
const escapedWord = escapeRegExp(word);
return new RegExp(`\\b([^a-z]+)?${escapedWord}([^a-z]+)?\\b`, 'i');
});
for (let i = 0; i < wordRegexs.length; i += 1) {
const regEx = wordRegexs[i];
const match = str.match(regEx);