0

How can I match array items against text and work with the found items (format in text and remove from array list) ?

I don't know the array and I don't know the text. But when an array item is contained in the text then party!

var arrString = 'apple, banana, monkey, sugar',
    text = "This a nice  monkey  zoo with  banana  trees.";

var arr = arrString.split(", ");

var arrMatch = "";

for(var i=0;i<arr.length;i++){
    if(text.search(arr[i])!=-1){
        arrMatch = arr[i];

        //format found item in text
        var text = text.replace(arrMatch, '<b>'+arrMatch+'</b>');

        //Remove found item from array <<<<<< Needs a fix
        if ( i !== -1 ) arr.splice(i, 1);
    }
}

if(arrMatch !== "") {
    $("body").append(arrString + '<br><br>The text contains the array item(s) "'+ arrMatch +'".');
}

var arrLeft = arr.join(", ");

$("body").append("<br><br><hr /><br>" + text + "<br><br>These array items are left: " + arrLeft);

Test: http://jsfiddle.net/Hxdht/3/

Note: This is a follow up to jQuery: Find array items in text string

Community
  • 1
  • 1
Martin
  • 2,007
  • 6
  • 28
  • 44
  • You know that `index = arr.indexOf(arrMatch);` is the same as just accessing `i`, right? – BenM Jan 12 '14 at 16:52
  • The `if()` serves no purpose. It'll never get executed because you explicitly start the loop off as `i = 0`... – BenM Jan 12 '14 at 16:58
  • But if I remove it, the wrong items are removed. – Martin Jan 12 '14 at 17:05
  • Why do you want to remove the item from the array? That will only mess with the for loop and give you all kinds of headaches. – fiskeben Jan 12 '14 at 17:11
  • This is just a simplified example. The unused ones are used for further programming. – Martin Jan 12 '14 at 17:52

2 Answers2

1

Try

for(var i=0;i<arr.length;i++){
    if(text.search(arr[i])!=-1){
        arrMatch = arr[i];
        text = text.replace(arrMatch, '<b>'+arrMatch+'</b>'); 
        //Remove found item from array <<<<<< Needs a fix
        if ( i !== -1 ){ arr.splice(i, 1); i--;}//decrement the index
    }
}

JSFiddle here

Nouphal.M
  • 6,304
  • 1
  • 17
  • 28
  • It might be nicer, though, to loop from the top down and not do any of this index fiddling: `for (var i = arr.length; i-->0;) {... arr.splice(1, 1) ... }`. – Scott Sauyet Jan 12 '14 at 18:14
1

A different technique (Fiddle):

var arrString = "apple, banana, monkey, sugar",
    text = "This a nice  monkey  zoo with  banana  trees.";

var arr = arrString.split(", ");

var found = [], foundIndices = [];
var test = new RegExp("\\b" + arr.join("\\b|\\b") + "\\b", "g");

var newText = text.replace(test, function(word) {
    if (found.indexOf(word) < 0) {
        found.push(word);
    };
    var index = arr.indexOf(word);
    if (foundIndices.indexOf(index) < 0) {
        foundIndices.push(index);
    }
    return "<b>" + word + "</b>";
});

foundIndices.sort(function(a, b) {return b - a;});
foundIndices.forEach(function(index) {arr.splice(index, 1);});

log(arrString);
log("The text contains " + found.join(", ") + ".");
log(newText);
log("These array itmes are left: " + arr.join(", ") + ".");

This will not work if the items to test have special characters important in regular expressions.

The big difference is that it builds a single regular expression and then goes through the string all at once replacing all the matches it finds.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • Thanks, but Nouphal.M's solution does just fine and seems more stable. I made it case insensitive and it works just fine so far. – Martin Jan 12 '14 at 17:48
  • That's fine. I wasn't really expecting you to use it. But you might want to look at my comment on the other answer. – Scott Sauyet Jan 12 '14 at 18:22