Here is a section of code used by CKEditor on my website:
以下是CKEditor在我的网站上使用的一段代码:
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.
我需要将所有CKEditor代码存储在javascript字符串中,但我无法弄清楚如何执行它,因为字符串中包含“和”。看到问题?此外,我认为我不能逃避引号,因为我尝试这样做,编辑器没有工作。
Any idea what I can do?
知道我能做什么吗?
3 个解决方案
#1
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
.
您可以尝试获取字符串并将JavaScript转义码注入其中。在使用格式时,JavaScript基本上可以使用任何unicode值:\ u #### - 因此,对于'字符,代码是\ u0039,对于“字符,代码是\ 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, becomes:
当以这种方式逃脱时,变为:
"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 come back down. Both languages use \ as their escape character, and it is possible to get funky scenarios which are brain-teasers to solve.
虽然这比第一个版本更具可读性 - 反序列化在跨越对象空间时可能有点棘手,例如将javascript对象传递给需要反序列化为对象的C#解析器,然后重新序列化回来了。两种语言都使用\作为它们的转义字符,并且有可能得到一些时髦的场景,这些场景是脑筋急转弯的解决方案。
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.
\ u ####方法的优点是只有JavaScript通常在典型的堆栈中使用它 - 因此很容易理解哪些部分应该由哪个应用程序块转义。
#2
0
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);
#3
0
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.
不需要使用Unicode转义序列。用双引号括起你的字符串,并在字符串中的任何双引号之前加一个反斜杠。
var x = "\"[acronym='Laugh Out Loud']lol[/acronym]\"";
#1
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
.
您可以尝试获取字符串并将JavaScript转义码注入其中。在使用格式时,JavaScript基本上可以使用任何unicode值:\ u #### - 因此,对于'字符,代码是\ u0039,对于“字符,代码是\ 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, becomes:
当以这种方式逃脱时,变为:
"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 come back down. Both languages use \ as their escape character, and it is possible to get funky scenarios which are brain-teasers to solve.
虽然这比第一个版本更具可读性 - 反序列化在跨越对象空间时可能有点棘手,例如将javascript对象传递给需要反序列化为对象的C#解析器,然后重新序列化回来了。两种语言都使用\作为它们的转义字符,并且有可能得到一些时髦的场景,这些场景是脑筋急转弯的解决方案。
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.
\ u ####方法的优点是只有JavaScript通常在典型的堆栈中使用它 - 因此很容易理解哪些部分应该由哪个应用程序块转义。
#2
0
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);
#3
0
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.
不需要使用Unicode转义序列。用双引号括起你的字符串,并在字符串中的任何双引号之前加一个反斜杠。
var x = "\"[acronym='Laugh Out Loud']lol[/acronym]\"";