最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Too many quotes within quotes -- what to do? - Stack Overflow

programmeradmin5浏览0评论

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?

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?

Share Improve this question edited Jun 20, 2012 at 22:38 Robert Harvey 181k48 gold badges348 silver badges513 bronze badges asked Jun 20, 2012 at 22:15 NateNate 28.6k39 gold badges136 silver badges228 bronze badges 3
  • 5 All your quotes are belong to quotes. – ThinkingStiff Commented Jun 20, 2012 at 22:19
  • 1 See stackoverflow./q/97578/102937 for escaping quotes in Javascript. – Robert Harvey Commented Jun 20, 2012 at 22:20
  • Also stackoverflow./questions/2004168 – Robert Harvey Commented Jun 20, 2012 at 22:21
Add a ment  | 

3 Answers 3

Reset to default 8

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:

\u0034[acronym=\u0039Laugh Out Loud\u0039]lol[/acronym]\u0034

Alternatively, you could attempt to simply escape the quotes as in:

\"[acronym=\'Laugh Out Loud\']lol[/acronym]\"

The problem here occurs when you wind up with this kind of situation:

"data:{'prop1':'<a href="/url/here/">back\\slash</a>'}"

Which, when escaped in this manner, bees:

"data:{\'prop\':\'<a href=\"/url/here/\">back\\\\slash</a>\'}\"

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 e 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.

hmm.. you said you already tried to escape the quotes and it gave problems.
This shouldn't give problems at all, so try this:

$newstring = addslashes($oldstring);

There's no need to use Unicode escape sequences. Just surround your string with double quotes, and put a backslash before any double quotes within the string.

var x = "\"[acronym='Laugh Out Loud']lol[/acronym]\"";
发布评论

评论列表(0)

  1. 暂无评论