views:

312

answers:

3

After upgrading to jquery 1.4.1 i noticed there was an error anytime i tried calling json.parse. The issue is part of the regex used in json. it uses a $ in the pattern that conflicts with JQuery's $ shortcut.

I don't want to use the non-conflict option with jquery because i have tons of places i'd have to replace the $ with the new corrected shortcut.

Is there a way to wrap a regex pattern in single quotes or something so the pattern string is handled as literally a string?

Broken section in json-2.0.js: (fails on the $)

if (/^[\],:{}\s]*$/.
test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
.....
}

Thanks

Update:

The problem was not as it appeared and didn't have to do with a $ conflict. From the OP:

The error was bombing on test.replace because the object that was passed in was already deserialized so the method replace was not found. I guess upgrading to JQuery 1.4.1 had some changes in the way the result object is handled on the success event of the $.ajax function.

+3  A: 

Are you sure it's failing on the $, and for that reason? Because that's a massive namespacing/parsing failure if so. There's no reason at all the JavaScript engine should be looking for an external symbol there. It's already encapsulated in the way you asked about (by the slashes, which are effectively quotes for a regex). If that were really the problem, it would be every bit as surprising as the interpreter choking on a $ inside a string. I think your problem lies elsewhere.

T.J. Crowder
+1  A: 

Here's a page that pulls in jQuery 1.4.1 and json2.js, and it calls JSON.parse(), and it gets no exceptions or errors: http://gutfullofbeer.net/json.html

Pointy
A: 

The error was bombing on test.replace the object that was passed in was already deserialized so the method replace was not found. I guess upgrading to JQuery 1.4.1 had some changes in the way the result object is handled on the success event of the $.ajax function.

This question should be deleted

Apparently this question cannot be deleted with the amount of answers that have been suggested.

used2could
I've taken the liberty of copying this to the question itself. (You can edit questions.)
T.J. Crowder
Thank you so much T.J.!
used2could