I have no idea why I am getting this and I have spent over a day trying to mess with the code. Now the simplest I can get is this (Trying to build a javascript array with an escaped string coming from a database):
vat test = '["<script>var bob = \\"This is ok\\"</script>", "<script>search.replace(/!|\\\?)/g, \\"\\")</script>"]';
So far, so good.
Then this breaks and returns the error:
JSON.parse(test);
To give an idea, the original 2 array items were this:
<script>var bob = "This is ok"</script>
<script>search.replace(/!|\?)/g, "")</script>
All the codes of any kind that were entered in the database have always worked, except now, because of that question mark from a user-entered line in my CMS. If I remove it or replace the question mark by anything else, it works.
I cannot change the regex so to make it work any other way, I am not the one who entered this. Plus this is a simplified version of a whole script, I just narrowed it to the question mark.
I also looked online if the question mark had a special meaning for json, did not find anything.
I wish I could have sent a fiddle instead of just code, but jsfiddle doesn't let me add the tag in the javascript window.
JSON.parse('["<script>var bob = \\"This is ok\\"</script>", "<script>search.replace(/!|\\\?)/g, \\"\\")</script>"]')
SyntaxError: Unexpected token ?
There are two unescapes going on here, so you need an extra \\
before the ?
JSON.parse('["<script>var bob = \\"This is ok\\"</script>", "<script>search.replace(/!|\\\\?)/g, \\"\\")</script>"]')
["<script>var bob = "This is ok"</script>", "<script>search.replace(/!|\?)/g, "")</script>"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.