简体   繁体   English

如何在javascript或jquery中对所有特殊字符进行编码和解码?

[英]How to encode and decode all special characters in javascript or jquery?

I want to be able to encode and decode all the following characters using javascript or jquery... 我希望能够使用javascript或jquery对以下所有字符进行编码和解码...

~!@#$%^&*()_+|}{:"?><,./';[]\\=-` 〜!@#$%^&*()_ + |} {:“?> <,。/'; [] \\ =-`

I tried to encode them using this... 我试图使用此编码...

var cT = encodeURI(oM); // oM holds the special characters
cT = cT.replace(/[!"#$%&'()*+,.\/:;<=>?@[\\\]^`{|}~]/g, "\\\\$&");

Which does encode them, or escape them rather, but then I am trying to do the reverse with this... 哪个确实对它们进行了编码,或者更确切地说,是对它们进行了转义,但是我试图与此相反。

decodeURIComponent(data.convo.replace(/\+/g, ' '));

But, it's not coming out in any way desired. 但是,它并没有以任何期望的方式出现。

I've built a chat plugin for jquery, but the script crashes if someone enters a special character. 我为jquery构建了一个聊天插件,但是如果有人输入特殊字符,脚本就会崩溃。 I want the special characters to get encoded, then when they get pulled out of the data base, they should be decoded. 我希望对特殊字符进行编码,然后在将其从数据库中拉出时,应对它们进行解码。 I tried using urldecode in PHP before the data is returned to the ajax request but it's coming out horribly wrong. 我尝试在PHP中使用urldecode,然后再将数据返回给ajax请求,但结果出了问题。

I would think that there exists some function to encode and decode all special characters. 我认为存在一些编码和解码所有特殊字符的功能。

Oh, one caveat for this is that I'm wrapping each message with html elements, so I think the decoding needs to be done server side, before the message is wrapped, or be able to know when to ignore valid html tags and decode the other characters that are just what the user wanted to type. 哦,有一点需要注意的是,我用html元素包装了每条消息,因此我认为解码需要在服务器端完成,然后再包装消息,或者能够知道何时忽略有效的html标签并解码。用户想要输入的其他字符。

Am I encoding/escaping them wrong to begin with? 我是从一开始就编码/转义错误吗?

Is that why the results are horrible? 这就是为什么结果如此可怕的原因吗?

This is pretty simple in javascript 这在javascript中非常简单

//Note that i have escaped the " in the string - this means it still gets processed
var exampleInput = "Hello there h4x0r ~!@#$%^&*()_+|}{:\"?><,./';[]\=-`";
var encodedInput = encodeURI(exampleInput);
var decodedInput = decodeURI(encodedInput);
console.log(exampleInput);
console.log(encodedInput);
console.log(decodedInput);

Just encode and decode the input. 只需对输入进行编码和解码。 If something else is breaking in your script it means you are not stripping away things that you are somehow processing. 如果脚本中有其他问题,则意味着您没有剥离正在处理的内容。 It's hard to provide an accurate answer as you can see encoding and decoding the URI standards does not crash things. 很难提供准确的答案,因为您可以看到URI标准的编码和解码不会崩溃。 Only the processing of this content improperly would cause issues. 仅对此内容的处理不当会引起问题。

When you output the content in HTML you should be encoding the HTML entities. 当您以HTML输出内容时,您应该对HTML实体进行编码。

Reference this thread Encode html entities in javascript if you need to actually encode for display inside HTML safely. 如果您需要实际编码以便在HTML内部安全显示,请引用此线程在javascript中对html实体进行编码。

An additional reference on how html entities work can be found here: W3 Schools - HTML Entities and W3 Schools - HTML Symbols 关于html实体如何工作的其他参考可以在这里找到: W3学校-HTML实体W3学校-HTML符号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM