1

I'm rewriting one of my scripts right now and encountered a problem I just can't figure out. command is an input variable and now I've run this test (both regular expressions are the same):

var parts = command.match(/([^\s"]+(?=\s*|$))|(".+?")/g);
console.log(command === "view -10 10 -10 10");
console.log(parts);
console.log(String("view -10 10 -10 10").match(/([^\s"]+(?=\s*|$))|(".+?")/g));

The console now says

true
[]
["view", "-10", "10", "-10", "10"]

This completely confuses me. Why does command not get separated the same way, when it equals my test string even when using ===?

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
Ingo Bürk
  • 19,263
  • 6
  • 66
  • 100
  • 2
    I tried your example in Chrome, and it works as expected. Which Browser are you using? – Gregor Sep 16 '12 at 10:48
  • You might want to look into whether command is being populated as-expected. Is it async? Is it server-side or user-facing? – Norguard Sep 16 '12 at 10:50
  • Perhaps it is a scope issue??? – Randy the Dev Sep 16 '12 at 10:52
  • You should try to reproduce your problem in a [fiddle](http://jsfiddle.net). You'll probably find your bug (not visible there) in the making. If not, this fiddle would make the question answerable. – Denys Séguret Sep 16 '12 at 10:53
  • I'm using FireFox. @Norguard: The comparison says true so it has to be the same, doesn't it? – Ingo Bürk Sep 16 '12 at 10:53
  • I just noticed that `parts` still is "filled" in the sense that I can access its elements. But why can't console.log show it the right way? It appears to be broken in some sense? – Ingo Bürk Sep 16 '12 at 10:53
  • Yes : `===` means same type and same chars. – Denys Séguret Sep 16 '12 at 10:54
  • I will try to reproduce it. But the funny thing is that I copypasted this code from my old script and it works perfectly in there. But I will see if I can reproduce it in a fiddle. – Ingo Bürk Sep 16 '12 at 10:55
  • Note that console.log [may be tricky](http://stackoverflow.com/questions/11214430/wrong-value-in-console-log/11214508#11214508). – Denys Séguret Sep 16 '12 at 10:56
  • @dystroy Thanks, I just noticed something else. This piece of code runs over a loop and if I just break the loop after the first turn, the output seems to be correct. However, `parts` is passed to a function within the loop and logging within that function renders the same problem. In fact, it causes the overall program to break. I have an idea that I#m going to try, give me a moment. – Ingo Bürk Sep 16 '12 at 10:59
  • Try `console.log(parts.slice(0))` or `console.log(JSON.parse(JSON.stringify(parts)))` - i.e., log a copy of the array. By the way, you don't need `String()` in that last log line, you can call `.match()` on a string literal. – nnnnnn Sep 16 '12 at 11:07
  • @ nnnnnn: Yes, that was the idea I was just talking about and indeed the called function in combination with the delayed logging mentioned by dystroy was responsible for the log result. I also just found the error in the called function which caused the whole thing to break. This makes this question solved. Thanks for your help and sorry for the confusion. This was a tricky little bug. – Ingo Bürk Sep 16 '12 at 11:11
  • 4
    I suggest you explain it in an answer and then accept your answer. This might help somebody's else later. – Denys Séguret Sep 16 '12 at 11:14
  • Well .. I would do that but unfortunately I can't for another 7 hours. I'll just edit it into the question. – Ingo Bürk Sep 16 '12 at 11:23
  • 1
    @Ingo dystroy added the solution for you, it would be nice if you could mark it as "accepted". – bfavaretto Sep 17 '12 at 14:28
  • Thanks, I will. I also created a jsfiddle to reproduce the scenario, I'll add that, too. Thanks! – Ingo Bürk Sep 18 '12 at 08:35

1 Answers1

1

From OP

Here's the solution to the whole problem: The basic structure of the program was as follows

while (<condition>) {
    var command = getNextCommand();

    var parts = command.match(/([^\s"]+(?=\s*|$))|(".+?")/g);
    processParts(parts);
}

wherein processParts() manipulated the argument:

function processParts(parts) {
    var foo = parts.shift();
    doSomethingElse(foo);
}

This caused parts in the main routine to shrink and in my code processParts actually shifted all elements, causing console.log(parts) to write an empty array as it was logged delayed (see dystroy's comment).

On top of that, my processParts() function had a mistake which I didn't notice and which is what I blamed the empty parts for. After fixing that mistake the above code worked again as I didn't need parts anymore and could live with it having shrunk. In general you might wanna watch out for that, though ... JavaScript does some weird stuff.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758