简体   繁体   English

当我在RegEx中转义<字符时,为什么jslint会抱怨?

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

The code: var foo = /\\</; 代码: var foo = /\\</;

When I go to jslint.com and enter that in, I get: 当我去jslint.com并输入时,我得到:

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

It used to tell me Unexpected escaped character '<' in regular expression. 它曾经告诉我Unexpected escaped character '<' in regular expression. but times have changed. 但时代变了。

So I'm just curious, why? 所以我只是好奇,为什么? If you try var foo = /\\>/; 如果你尝试var foo = /\\>/; it doesn't care, what's the deal with < ? 它不在乎,与< ?有什么关系?

If you have a two simple divs, the difference is clear: 如果你有两个简单的div,差别很明显:

<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. JSLint不假定脚本是独立文件或HTML文档的脚本标记内。 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. 在诸如XULMXMLXAMLTVMLLZXXHTML5SVG等标记语言中,脚本标记内容被视为上例中的第一个div,因此左尖括号看起来像标记的开头。 In those languages, use an entity replacement or a CDATA block to wrap the left angle bracket and ampersand characters: 在这些语言中,使用实体替换或CDATA块来包装左尖括号和&符号字符:

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

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

References 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM