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?
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?
Share Improve this question edited Jun 12, 2014 at 15:40 Ashima asked May 27, 2013 at 19:07 AshimaAshima 4,8346 gold badges41 silver badges64 bronze badges 2- 1 What's your exact goal ? – Denys Séguret Commented May 27, 2013 at 19:08
- 1 My goal is to find out if the given string to my method as a parameter is HTML escaped or not? – Ashima Commented May 27, 2013 at 19:16
2 Answers
Reset to default 6I found that using
escape(unescape(str))
will always provide an escaped string. And the unescape string will do nothing unless the string itself contains escaped expressions.
Note: should have used encodeURI(decodeURI(str)) instead as escape is now depreciated.
As "hello"==escape("hello")
, no, you can't at all guess if escaping was applied.
If you want to know if it's probable that the string has been escaped, then you might test
var wasProbablyEscaped = /%\d\d/.test(str);
var wasProbablyNotEscaped = !wasProbablyEscaped && /%\d\d/.test(escape(str));
as escaping adds %
followed by two digits when something has to be escaped. But you can't be totally sure as some strings don't change when you escape them.
In your case, I'd probably advise you not to escape if wasProbablyEscaped
is true.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744763634a4592319.html
评论列表(0条)