2017-08-02 19:43:22 +00:00
|
|
|
export function removePunctuationFromString (str) {
|
2019-10-10 18:11:50 +00:00
|
|
|
return str.replace(/[.,/#!@$%^&;:{}=\-_`~()]/g, ' ');
|
2017-08-02 19:43:22 +00:00
|
|
|
}
|
|
|
|
|
|
2020-04-28 14:47:52 +00:00
|
|
|
// NOTE: the wordsToMatch aren't escaped in order to support regular expressions,
|
|
|
|
|
// so this method should not be used if wordsToMatch contains unsanitized user input
|
2017-08-02 19:43:22 +00:00
|
|
|
export function getMatchesByWordArray (str, wordsToMatch) {
|
2019-10-08 14:57:10 +00:00
|
|
|
const matchedWords = [];
|
2020-04-28 14:47:52 +00:00
|
|
|
const wordRegexs = wordsToMatch.map(word => new RegExp(`\\b([^a-z]+)?${word}([^a-z]+)?\\b`, 'i'));
|
2017-08-02 19:43:22 +00:00
|
|
|
for (let i = 0; i < wordRegexs.length; i += 1) {
|
2019-10-08 14:57:10 +00:00
|
|
|
const regEx = wordRegexs[i];
|
|
|
|
|
const match = str.match(regEx);
|
2017-08-02 19:43:22 +00:00
|
|
|
if (match !== null && match[0] !== null) {
|
2019-10-08 14:57:10 +00:00
|
|
|
const trimmedMatch = removePunctuationFromString(match[0]).trim();
|
2017-08-02 19:43:22 +00:00
|
|
|
matchedWords.push(trimmedMatch);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return matchedWords;
|
|
|
|
|
}
|