I'm facing a problem with Angular/Typescript, but it can be applied on the most of programming languages.
I have a huge text that we will call fulltext and a set of filters that are applied on fulltext and know if they match; let me explain better:
suppose our fulltext is something like this
"Hello there! My name is Mattia"
and the filters are
["my name", "is Mattia"]
First of all, I perform an operation of stamming and cleaning the fulltext and the filters, applying regex and replace.
document.snippet.toLowerCase().replace(/[^A-Z0-9]/ig, "");
filter.toLowerCase().replace(/[^A-Z0-9]/ig, "");
So now I can avoid various false positives such as the mismatch between "Mattia" and "mattia" etc...
At this point the answer can be trivial: I just scroll the list of filters and use the "include" string method. But suppose you have "mat" instead of "mattia" as a filter. Include uses substring, so it would return true in any case.
In my context, I have some objects called "sections" numbered through romanic numbering (i, ii, iii, ...) contained within the fulltext. So, where a "section i" filter is applied, the method in question must return false if present "... section ii ...", "... section iii ...".
I almost forgot, all filters must be satisfied, so if even one does not match, the method must return false.
I hope I've made the problem as clear as I can.
I used answer of Jan Jakub Nanista and wrote a function and added case ignorance in it.
checkFilters(inputString: string, filters: string[]) : boolean {
filters.forEach((filter) => {
const regexp = new RegExp('\\b' + filter + '\\b', 'ig');
if(!regexp.test(inputString)){
return false;
}
})
return true;
}