javascript - How to find out if a given string is HTML Escaped or not? - Stack Overflow

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

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
Add a ment  | 

2 Answers 2

Reset to default 6

I 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信