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 ===?
From OP
Here’s the solution to the whole problem:
The basic structure of the program was as follows
wherein
processParts()manipulated the argument:This caused
partsin the main routine to shrink and in my codeprocessPartsactuallyshifted all elements, causingconsole.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 emptypartsfor. After fixing that mistake the above code worked again as I didn’t needpartsanymore and could live with it having shrunk. In general you might wanna watch out for that, though … JavaScript does some weird stuff.