java: unclosed string literal,不良逃生未封闭的字符串

var freebie = ' \

\
\

'+item%5B'thumb'%5D%5B0%5D+' \

\
';

Does anyone know why this is returning 2 errors in JSLint?

Here's an image of the 2 errors:

fYT3Q.png

解决方案

Short answer, you need to configure JSLint to tolerate ECMAScript 5.

This can be done with:

/*jslint es5: true, all-your-other-jslint-options */

Long answer:

The sequence, \ followed by a line terminator, was not actually valid Javascript until recently (version 5, December 2009).

From the previous iteration (version 3) of the ECMAScript (ECMA-262) standard, section 7.8.4 states: (with some irrelevant entries removed):

StringLiteral ::

" DoubleStringCharacters(opt) "

' SingleStringCharacters(opt) '

SingleStringCharacters ::

SingleStringCharacter SingleStringCharacters(opt)

SingleStringCharacter ::

SourceCharacter but not single-quote ' or backslash \ or LineTerminator

\ EscapeSequence

So the sequence you have ends up at that last line above, a single \ followed by the syntax element EscapeSequence. Examining that further:

EscapeSequence ::

CharacterEscapeSequence

0 [lookahead ∉ DecimalDigit]

HexEscapeSequence

UnicodeEscapeSequence

CharacterEscapeSequence ::

SingleEscapeCharacter

NonEscapeCharacter

SingleEscapeCharacter :: one of

' " \ b f n r t v

NonEscapeCharacter ::

SourceCharacter but not EscapeCharacter or LineTerminator

EscapeCharacter ::

SingleEscapeCharacter

DecimalDigit

x

u

HexEscapeSequence ::

x HexDigit HexDigit

UnicodeEscapeSequence ::

u HexDigit HexDigit HexDigit HexDigit

Since the next character after \ is neither 0, x or u, the only alternative is CharacterEscapeSequence which boils down to either SingleEscapeCharacter (not the case since the line terminator is not one of those characters listed ) or NonEscapeCharacter (which explicitly excludes the line terminator as a possibility).

There's also this note at the bottom of that section:

NOTE: A 'LineTerminator' character cannot appear in a string literal, even if preceded by a backslash . The correct way to cause a line terminator character to be part of the string value of a string literal is to use an escape sequence such as \n or \u000A.

Now, ECMAScript 5 changed that a little. Starting from there, they modified the definition of SingleStringCharacter thus:

SingleStringCharacter ::

SourceCharacter but not one of ' or \ or LineTerminator

\ EscapeSequence

LineContinuation

LineContinuation ::

\ LineTerminatorSequence

and modified the note to be:

NOTE: A line terminator character cannot appear in a string literal, except as part of a LineContinuation to produce the empty character sequence. The correct way to cause a line terminator character to be part of the String value of a string literal is to use an escape sequence such as \n or \u000A.

And, rather than break JSLint for all the current scripts out there, the authors intelligently decided to make ECMAScript 5 support optional, requiring a change to the JSLint options to activate it. That way, it will only allow ECMAScript 5 if you explicitly tell it so.

You can visit the http://www.jslint.com/ website and confirm this:

Code:

var xyzzy = ' \

hello";

Error:

Problem at line 1 character 16: This is an ES5 feature.

var xyzzy = ' \

Problem at line 2 character 13: Unclosed string.

hello";

Problem at line 2 character 13: Stopping. (66% scanned).

If you then scroll down to the flags section, there's an entry for Tolerate ES5 syntax which will, when set, remove that error.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值