I have a large string(1000s of words) which i want to compare with all the elements of an array, which contains large strings as well, for all 3 or more consecutive word match. I have implemented it with regex but getting blank matched array.
Example with smaller text:
let textToCompare = "Hello there how are you doing with your life";
let textsToCompareWith= [
{ id:1, text:"Hope you are doing good with your life" },
{ id:2, text:"what are you doing with your life. hello there how are you" },
{ id:3, text:"hello there mate" }
];
Expected Output:
[
{id:1, matchedText:["with your life"]},
{id:2, matchedText:["are you doing with your life","hello there how are you"]},
{id:3, matchedText:[]}
];
Current Output:
[
{id:1, matchedText:[]},
{id:2, matchedText:[]},
{id:3, matchedText:[]}
];
My Code:
let regex = new RegExp("\b" + textToCompare.split(" ").join("\b.*\b") + "\b", "gi");
let output = textsToCompareWith.map(textObj => {
// Match against each element in the array
let matchedText = textObj?.text.match(regex);
console.log(matchedText);
return {
id: textObj.id,
matchedText: matchedText ? matchedText : [] // Return an empty array if no match is found
};
});
console.log(output);
2
Answers
You could check each word with each other and keep an eye on the last word.
A slightly different approach by avoiding use words.
I created an answer just for my own learning of JavaScript. Piecing stuff together, I came up with:
I’m pretty sure this code will have many flaws and would look cluncky, but the idea is to split your input in chuncks of 3+words based on the assumption it’s splittable by whitespaces. I then tried to sort the resulting array on length so that we will not find smaller substrings first.
Who knows, maybe something in here is actually usable.