简体   繁体   中英

escape string (% character) in encodeURI in JavaScript

I have a problem where I need to preserve %2F in a URI string. However, encodeURI encodes % as %25 (as normal), so the whole string ends up being %252F instead of %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. 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. This way, every double encoding is consistently handled.

For example:

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

will return

'http://abcd.com'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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