我正在JavaScript中处理utf-8字符串并需要对其进行转义。
在我的浏览器中,escape() / unescape() 和 encodeURI() / decodeURI()均可用。
escape()
> var hello = "안녕하세요"
> var hello_escaped = escape(hello)
> hello_escaped
"%uC548%uB155%uD558%uC138%uC694"
> var hello_unescaped = unescape(hello_escaped)
> hello_unescaped
"안녕하세요"
encodeURI()
> var hello = "안녕하세요"
> var hello_encoded = encodeURI(hello)
> hello_encoded
"%EC%95%88%EB%85%95%ED%95%98%EC%84%B8%EC%9A%94"
> var hello_decoded = decodeURI(hello_encoded)
> hello_decoded
"안녕하세요"
虽然 encodeURI() 和 decodeURI() 可以处理上述 utf-8 字符串,但文档(以及函数名称本身)告诉我这些方法是用于 URI 的;我没有在任何地方看到有提到 utf-8 字符串的内容。
简单来说,使用 encodeURI() 和 decodeURI() 处理 utf-8 字符串是否可行?
encodeURI()
不使用 UTF-8,而escape()
使用 UTF-8。 - KingRider