- Joined
- Mar 1, 2014
- Messages
- 206
- Reaction score
- 95
- First Language
- English
- Primarily Uses
- RMMV
Just found and interesting but potentially brain scrambling bug involving using Regular expressions recursively. I consider it a potential trap for new players, (don't know what forum this fits into)
take this source string
<commandA: 10>
<commandA: 9>
<commandA: 5>
<commandA: 4>
<commandA: 3>
And the fucntion contain a For loop and a Regular expression.
and the output is
[Object] <commandA: 10>
Null <commandA: 9>
[Object] <commandA: 8>
Null <commandA: 5>
[Object] <commandA: 4>
Null <commandA: 3>
As you can tell the strings should be matching but they are returning null?
Why? Well apparently Firefox and Apparently RPGmakers browser engine have this weird bug that cause the RegExp object to not update or something.
Another way to invoke this bug is to call exec on a Regular Expression Literal within the loop it self
this produces the same bug on firefox.
to work around this thing is to declare a variable within the for loop and Then call exec() on that,
now the output is
[Object] <commandA: 10>
[Object] <commandA: 9>
[Object] <commandA: 8>
[Object] <commandA: 5>
[Object]<commandA: 4>
[Object] <commandA: 3>
Anyone else experience this or is it just me?
take this source string
<commandA: 10>
<commandA: 9>
<commandA: 5>
<commandA: 4>
<commandA: 3>
And the fucntion contain a For loop and a Regular expression.
Code:
var commands = GetCommands(this.commandData); //gets string array
var regEXP = /<([^<>:]*):?[^>]*>/ig;
for (var i = 0; i < commands.length; i++) {
var line = commands[i].parameters[0];
var match = regEXP.exec(line);
console.log(match + "," + line);
if (match && line) {
this.parseCommand(match, line);
}
}
[Object] <commandA: 10>
Null <commandA: 9>
[Object] <commandA: 8>
Null <commandA: 5>
[Object] <commandA: 4>
Null <commandA: 3>
As you can tell the strings should be matching but they are returning null?
Why? Well apparently Firefox and Apparently RPGmakers browser engine have this weird bug that cause the RegExp object to not update or something.
Another way to invoke this bug is to call exec on a Regular Expression Literal within the loop it self
Code:
for (var i = 0; i < commands.length; i++) {
var line = commands[i].parameters[0];
var match = /<([^<>:]*):?[^>]*>/ig.exec(line);
console.log(match + "," + line);
if (match && line) {
this.parseCommand(match, line);
}
}
to work around this thing is to declare a variable within the for loop and Then call exec() on that,
Code:
for (var i = 0; i < commands.length; i++) {
var regEXP = /<([^<>:]*):?[^>]*>/ig;
var line = commands[i].parameters[0];
var match = regEXP.exec(line);
console.log(commands[i]+ "," + match + "," + line);
if (match && line) {
this.parseTournamentCommand(match, line);
}
}
[Object] <commandA: 10>
[Object] <commandA: 9>
[Object] <commandA: 8>
[Object] <commandA: 5>
[Object]<commandA: 4>
[Object] <commandA: 3>
Anyone else experience this or is it just me?
