6

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

3
  • 5
    There's no need to escape the "<" character there. Also JSLint is just the codification of Crockford's random opinions. Think of it as a weird dude at a bus stop complaining to you about your code.
    – Pointy
    Nov 5, 2011 at 9:23
  • I know there's no need to escape it, hence the error. But why that character in particular...out of all the unnecessary characters to escape, why <. Nov 5, 2011 at 9:32
  • I'm sure there's a reason for his decision to include that warning in his application. Even if the reason is "it's stupid and confusing." Nov 5, 2011 at 9:33

1 Answer 1

3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.