1

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]);
         }
      }
   }
}
ChrisWness
  • 123
  • 1
  • 11
  • possible duplicate of [RegExp and special characters](http://stackoverflow.com/questions/23349041/regexp-and-special-characters) – Sean Airey Jun 10 '14 at 15:51
  • You might also ber intgerested in [escaing](http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript/3561711) the inventory items as you add them to the regex; Also, I would recommend using `[1,2,3]` array literals instead of `new Array(1,2,3)` – hugomg Jun 10 '14 at 15:57
  • Thanks, but I still need an answer first! – ChrisWness Jun 10 '14 at 15:59

2 Answers2

2
new RegExp("\\b" + objectList[i] + "\\b")

should work (you won't need the g flag since you're only testing and not looping through all the results). Remember to escape the backslashes.


On the other hand, why not

if (objectList.indexOf(entry) > -1) {
    if (inventory.indexOf(entry) > -1) {
        ...
    } else {
        ...
    }
}

? If you need to handle multiple items in an entry, just split the entry into single words:

entries = entry.split(" ")

and then loop through that array:

for (var entry in entries) {
    if (objectList.indexOf(entry) > -1) {
        if (inventory.indexOf(entry) > -1) {
            ...
        } else {
            ...
        }
    }
}
Albert Xing
  • 5,620
  • 3
  • 21
  • 40
  • That worked, thanks a lot! :) Would you mind explaining for me why '"\b" + objectList[i] + "\b"' didn't work? Just for my own education. – ChrisWness Jun 10 '14 at 16:11
  • 1
    If you enter `\b` as a string, you're literally trying to enter a backslash-escaped special character (like `\n` for newline). Try this yourself: enter `"\b"` into a JS console, and you'll get an "empty" string. Then enter `"\\b"`, and you'll see the string `"\b"` which is what you want the RegExp to contain. – Albert Xing Jun 10 '14 at 16:18
1

If I were you I would try top avoid using dynamically created regexes and try to go for something simpler. One way would be to write a simgle static regex that goes through each word and then check your inventory using plain string comparison.

var reg = /\w+/g;
var result = null;
while(result = reg.exec(entry)){
    var word = result[0].toLowerCase();
    for(var i=0; i<objectList.length; i++){
        if( objectList[i].toLowerCase() === word){
            /*Found it!*/
        }
    }
}
hugomg
  • 68,213
  • 24
  • 160
  • 246