Here is a section of code used by CKEditor on my website:
CKEDITOR.config.IPS_BBCODE = {"acronym":{"id":"8","title":"Acronym","desc":"Allows you to make an acronym that will display a description when moused over","tag":"acronym","useoption":"1","example":"[acronym='Laugh Out Loud']lol[/acronym]", ...
If you scroll to the right just a little, you will see this:
"[acronym='Laugh Out Loud']lol[/acronym]"
I need to store all of the CKEditor code inside a javascript string, but I can’t figure out how to do it because the string has both ” and ‘ in it. See the problem? Furthermore, I don’t think I can just escape the quotes because I tried doing that and the editor didn’t work.
Any idea what I can do?
You might try taking the string and injecting JavaScript escape codes into it. JavaScript can essentially use any unicode value when using the format:
\u####– so, for a'character, the code is\u0039, and for the"character, the code is\u0034.So – you could encode your example portion of the string as:
Alternatively, you could attempt to simply escape the quotes as in:
The problem here occurs when you wind up with this kind of situation:
Which, when escaped in this manner, becomes:
While this is somewhat more readable than the first version – de-serializing it can be a little tricky when going across object-spaces, such as a javascript object being passed to a C# parser which needs to deserialize into objects, then re-serialize and come back down. Both languages use \ as their escape character, and it is possible to get funky scenarios which are brain-teasers to solve.
The advantage of the
\u####method is that only JavaScript generally uses it in a typical stack – so it is pretty easy to understand what part should be unescaped by what application piece.