Why does jslint complain when I escape the < character in a RegEx?

安稳与你 提交于 2019-12-21 15:17:33

问题


The code: var foo = /\</;

When I go to jslint.com and enter that in, I get:

Problem at line 1 character 12: Unexpected '\'.

It used to tell me Unexpected escaped character '<' in regular expression. but times have changed.

So I'm just curious, why? If you try var foo = /\>/; it doesn't care, what's the deal with <?


回答1:


If you have a two simple divs, the difference is clear:

<div> < </div> <!--Missing right angle bracket, bad markup-->
<div> > </div> <!--No problem, just a greater than text node-->

JSLint does not assume that the script is a standalone file or inside a script tag of an HTML document. In markup languages such as XUL, MXML, XAML, TVML, LZX, XHTML5 or SVG, the script tag content is treated like the first div in the above example, so the left angle bracket will look like the start of a tag. In those languages, use an entity replacement or a CDATA block to wrap the left angle bracket and ampersand characters:

<script> 
if ( -1 &lt; 0 &amp;&amp; true !== false) {} 
</script>

<script>
<![CDATA[
if ( -2 < 0 && true !== false) {}
]]>
</script>

References

  • Escaped Markup Considered Harmful


来源:https://stackoverflow.com/questions/8019216/why-does-jslint-complain-when-i-escape-the-character-in-a-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!