Search code examples
javascriptregexdebuggingfirebug

Why does this error- and warning-free statement not complete?


I'm making a simple JavaScript for opening a dialog with a hypothetical recruiter. When I run the script it does not complete, but neither throws any errors or warnings. At least (!) it doesn't seem like it completes, but it might be me not thinking straight about how to use Firebug.

Here's the code:

var str = prompt("How are you today?", "I'm quite alright, thank you")
if (str.search(/\+alright\i|\+good\i|\+fine\i|\+alright\i/) != -1)                                                          {
  alert("I'm very glad to hear that! Me, I'm feeling freaky today!");                                               
} else if (str.search(/\+not/&&(/\+well|\+good/)||(/\+sad|\+down|\+blue/)) != -1)                                           {
  if (confirm("I'm sorry to hear that! Perhaps you'd like to get together and talk sometime?"))                         {
    var sadNumber = prompt("What's your phone number? (The one entered is mine, feel free to call me!)", "072-");
    if (sadNumber.search(/\D/) != -1) {
      alert("Sorry, I think there's something wrong with your number. Try entering it with just numbers please!");
      sadNumber = prompt("What's your phone number? (The one entered is mine, feel free to call me!)", "072-");
    } else {
      alert("That's fine! Let's move on with our job questions, shall we?");
    }
  } else if (alert("Wow! I didn't expect that answer! Truly interesting"));
}

Tried methods

This is what it looks like in Firebug:

After running, this is where the statement stops for some reason. Pressing continue breaks and steps out of the statement:

Step debugging in Firebug halted at line 9

Stepping through, the statement continues to run, but skips all the (I conbsider) important parts. As you can see here, the alert is being skipped, and statements continues at the else if line:

Step debugging in Firebug halted at line 11

My guess it that my regexp search (either the method, pattern, or modifiers) is wrong and that's why the statement breaks. I'm still finding it odd, though, since regexp errors usually throw errors or warnings, but this script returns none.

Anyone knows why this particular script breaks? Anyone having a good methodology for debugging errors that do not throw errors or warnings?


Solution

  • Your regular expressions are wrong.

    This one, for instance: /\+alright\i|\+good\i|\+fine\i|\+alright\i/ searches for +alrighti (literally) or +goodi or +finei or +alrighti, because \+ means a literal + and \i means a literal i.

    You probably meant /alright|good|fine/i, which searches for alright, good, or fine, case-insensitve. Or perhaps /\b(?:alright|good|fine)\b/i, which does the same but is expecting word boundaries on either side of the word.

    You can test out your regex at various sites, including regex101.com.