I'm still a newbie, and I'm creating a little function that picks up objects and adds them to an inventory when entered in a text box.
I have a text box (id="commandBox") and a button which launches the function 'pickUp()'. The function itself works fine but when I create the RegExp object using an array index, I don't know how to add a word boundary (\b) correctly. At the moment, when the array index concerned is 'pen', it also matches the 'pen' in 'PENcil' and 'sharPENer. How do I add the special character correctly?
I have **'d the line in question.
objectList = new Array("pencil","pen","rubber","sharpener");
inventory = new Array();
function pickUp(){
var entry = document.getElementById("commandBox").value;
var resultBox = document.getElementById("result");
for(i=0;i<objectList.length;i++){
**objectSearch = new RegExp(objectList[i],"g");**
if(inventory.indexOf(objectList[i])!=-1 && objectSearch.test(entry)==true ){
resultBox.innerHTML = resultBox.innerHTML + "<br />You have already picked up the " + objectList[i];
return;
}else{
if(objectSearch.test(entry)==true){
resultBox.innerHTML = resultBox.innerHTML + "<br />You picked up a " + objectList[i];
inventory.push(objectList[i]);
}
}
}
}