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

5
  • You know that index = arr.indexOf(arrMatch); is the same as just accessing i, right? Commented Jan 12, 2014 at 16:52
  • The if() serves no purpose. It'll never get executed because you explicitly start the loop off as i = 0... Commented Jan 12, 2014 at 16:58
  • But if I remove it, the wrong items are removed. Commented Jan 12, 2014 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. Commented Jan 12, 2014 at 17:11
  • This is just a simplified example. The unused ones are used for further programming. Commented Jan 12, 2014 at 17:52

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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) ... }.
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.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.