It's because you have to escape the parentheses around the gamer tag with a backslash, but as you're creating a RegExp with a string, you first have to escape the backslash itself too.
code:
var XBG = NewPsm.match(new RegExp("Xbox 360: (.*?) \\(" + X360GamerTag + "\\)", "i"))[1];
The reason for this is if you don't escape the parentheses, it'll capture what's between those too:
code:
Resulting RegExp: Xbox 360: (.*?) (Stefan Leroux)
Result:
[0] = Xbox 360: Sonic The Hedgehog 2 Stefan Leroux
[1] = Sonic The Hedgehog 2
[2] = Stefan Leroux
- The matched part is stored at index 2, even when you already knew that part as you provided that data yourself!
- It won't look for parentheses, it looks for "Stefan Leroux" immediately after "Sonic The Hedgehog 2".
So what did we learn today? Escape any characters which can be interpreted by the regular expression engine as special characters.
As for your second question: check if "reply" is null first before checking "reply[1]". That should do the trick.