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]);
         }
      }
   }
}
3
  • possible duplicate of RegExp and special characters Commented Jun 10, 2014 at 15:51
  • You might also ber intgerested in escaing 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) Commented Jun 10, 2014 at 15:57
  • Thanks, but I still need an answer first! Commented Jun 10, 2014 at 15:59

2 Answers 2

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 {
            ...
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That worked, thanks a lot! :) Would you mind explaining for me why '"\b" + objectList[i] + "\b"' didn't work? Just for my own education.
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.
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!*/
        }
    }
}

1 Comment

You probably mean var i = 0?

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.