简体   繁体   English

JavaScript 中 encodeURI 中的转义字符串(% 字符)

[英]escape string (% character) in encodeURI in JavaScript

I have a problem where I need to preserve %2F in a URI string.我有一个问题,我需要在 URI 字符串中保留 %2F。 However, encodeURI encodes % as %25 (as normal), so the whole string ends up being %252F instead of %2F.但是,encodeURI 将 % 编码为 %25(正常),因此整个字符串最终是 %252F 而不是 %2F。 How can I escape this so the % doesn't get encoded.我怎样才能逃避这个,所以 % 不会被编码。 This is happening deep within a framework, so JS manipulation is not an option, it needs to be done via string escaping.这发生在框架的深处,所以 JS 操作不是一个选项,它需要通过字符串 escaping 来完成。 I've tried using \ in a number of ways, none of it is working.我已经尝试以多种方式使用 \ ,但都没有奏效。

If you have control during the decode process, an unescape() before decodeURI would help.如果您在解码过程中有控制权,那么decodeURI之前的unescape()会有所帮助。 This way, every double encoding is consistently handled.这样,每个双重编码都会得到一致的处理。

For example:例如:

decodeURIComponent(unescape("http%253A%252F%252Fabcd.com")); decodeURIComponent(unescape("http%253A%252F%252Fabcd.com"));

will return将返回

'http://abcd.com' 'http://abcd.com'

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

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