如何判断给定的字符串是否是 HTML 转义的?

Cut*_*nja 2 html javascript string escaping

有没有什么方法可以判断给定的字符串是否是 HTML 转义的?

考虑以下 JavaScript 代码:

<script>
var str="hello";
var str_esc=escape(str);

document.write(isHTMLEscaped(str)) // *Should print False*

document.write(isHTMLEscaped(str_esc)); // *Should print True*

</script>
Run Code Online (Sandbox Code Playgroud)

在上述情况下是否有相当于isHTMLEscaped的方法?

Joe*_*Joe 6

我发现使用

escape(unescape(str))
Run Code Online (Sandbox Code Playgroud)

将始终提供转义字符串。除非字符串本身包含转义表达式,否则 unescape 字符串不会执行任何操作。

注意:应该使用encodeURI(decodeURI(str))代替,因为转义现在已被弃用。