I've got a chunk of code that is doing something really strange.
function getQuerystring(key, default_){
if (default_==null) default_="";
key = key.replace(/[\[]/,'\\\[').replace(/[\]]/,'\\\]');
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
var qs = regex.exec(window.location.href);
if(qs == null)
return default_;
else
return qs[1];
}
var isThankyou = getQuerystring('CONFIRMATION');
This function checks the URL for a parameter (in this case CONFIRMATION). From what I can tell everything is correct, but when the browser loads the code it throws an error in the console.
Uncaught SyntaxError: Invalid regular expression: missing /
Ultimately this chunk of code is used to determine if a user is on the confirmation page of the URL. If they are then it triggers some google analytics code to track ecommerce purchase information in GA.
The line that is giving me trouble is:
key = key.replace(/[\[]/,'\\\[').replace(/[\]]/,'\\\]');
When I load the page in the browser and look at the source this is what shows up:
key = key.replace(/[\[]/,'\\\[').replace(/[\/,'\\\]');
with two ]] showing up just before
Here's the weird thing. When I duplicate the line commenting out the first one, the error doesn't get thrown (the ]] still shows up however):
//key = key.replace(/[\[]/,'\\\[').replace(/[\]]/,'\\\]');
key = key.replace(/[\[]/,'\\\[').replace(/[\]]/,'\\\]');
I've tried everything I can think of with no success, I've even tried unchaining the replace method in case that was causing the issue.
key = key.replace(/[\[]/,'\\\[');
key = key.replace(/[\]]/,'\\\]');
Which results in the following in the browser source:
key = key.replace(/[\[]/,'\\\[');
key = key.replace(/[\/,'\\\]');
In any case, the issue seems to be with the second .replace not escaping the one ] or even keeping the two ]] for some reason.
I've also tried erasing cache, cookies, and turning off all plugins. I've tested in Chrome, Firefox, and Safari with the same results on all 3 browswers.
Here is the page that you can see the full block of code (last in the head): https://secure2.convio.net/ccod/site/Ecommerce?store_id=2841
window.location.href?