testTextByAnyWord.ts
// NOTE: v2. Совпадение по хотябы одному совпадению
export const testTextByAnyWord = ({ text, words }: { text: string, words: string[] }): boolean => {
const modifiedWords = words.join(' ').replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
const regexpGroups = modifiedWords.split(' ').map((w) => ['(?=.*' + w + ')'])
const regexp = new RegExp('^' + regexpGroups.join('|') + '.*$', 'im')
return regexp.test(text)
}testTextByAllWords.ts
import { getNormalizedWords } from './getNormalizedWords'
// NOTE: v1. Совпадение по всем совпадениям
export const testTextByAllWords = ({ text, words }: { text: string, words: string[] }): boolean => {
const modifiedWords = getNormalizedWords(words)
// Split your string at spaces & Encapsulate your words inside regex groups:
const regexpGroups = modifiedWords.split(' ').map((w) => ['(?=.*' + w + ')'])
// Create a regex pattern:
const regexp = new RegExp('^' + regexpGroups.join('') + '.*$', 'im')
return regexp.test(text)
}getNormalizedWords.ts
export const getNormalizedWords = (words: string[]): string =>
words.join(' ').replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
export const getNormalizedWordsArr = (words: string[]): string[] =>
getNormalizedWords(words).replace(/:/g, '').split(' ')