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