2

Is there any method to find out if the given string is HTML Escaped or not?

Consider the following javascript code:

<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>

Is there any method equivalent to isHTMLEscaped in the above case?

4

2 回答 2

4

我发现使用

escape(unescape(str))

将始终提供转义字符串。除非字符串本身包含转义表达式,否则 unescape 字符串将什么也不做。

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

于 2014-02-25T10:47:50.180 回答
1

因为"hello"==escape("hello"),不,您根本无法猜测是否应用了转义。

如果您想知道字符串是否可能已被转义,那么您可以测试

var wasProbablyEscaped = /%\d\d/.test(str);
var wasProbablyNotEscaped = !wasProbablyEscaped && /%\d\d/.test(escape(str));

当必须转义某些内容时,转义添加%后跟两位数。但是您不能完全确定,因为某些字符串在您转义它们时不会改变。

wasProbablyEscaped就您而言,如果是真的,我可能会建议您不要逃避。

于 2013-05-27T19:08:44.413 回答