这个正则表达式有什么问题? JSLint 抛出错误
What's wrong with this regex? JSLint is throwing an error
我正在尝试验证我的整个脚本 through the new JSLint,但是:
function a() {
'use strict';
var string;
string = string.replace(/\x00*/g, '');
}
它抛出:
Unexpected 'x' after '\'.
string = string.replace(/\x00*/g, '');
旧版本不会引发任何错误。我知道它处于测试阶段,但我希望它能像旧版本一样好用。顺便说一句,\x00
代表空字符。
那么,ECMAScript 6 是否改变了什么?它只是一个 JSLint 错误吗?我真的做错了什么吗?
The "A regular expression literal can be confused with '/='" error is thrown when JSLint, JSHint (prior to version 1.0.0) or ESLint encounters a regular expression literal that begins with the = character. In the following example we attempt to assign a regular expression literal to match the string "=1" to the variable x:
This error is raised to highlight a potentially confusing piece of code. Your code will run fine if you do not fix this error, but it may be confusing to others, especially at first glance to someone quickly searching through your script.
The / character is ambiguous in JavaScript. It can either signify the start or end of a regular expression literal, as it does in the example above, or it can be interpreted as the division operator. Like most of the arithmetic operators, the division operator can be combined with the assignment operator to produce a shorthand:
https://jslinterrors.com/a-regular-expression-literal-can-be-confused-with
所以你需要使用RegExp
构造函数:
string.replace(new RegExp('\x00*', 'g'), '');
输出与正则表达式文字相同的正则表达式:
console.log(new RegExp('\x00*', 'g').toString() === /\x00*/g.toString()); // true
提示
NULL 字符 \x00
可以缩短为 [=15=]
(MDN docs)
new RegExp('\0*', 'g')
--
更新
@nhahtdh 的回答表明你可以使用 /\u0000*/g
literal.
根据 ECMAScript 规范:
\x00
有效,语法展开下:
Atom -> \ AtomEscape
AtomEscape -> CharacterEscape
CharacterEscape -> HexEscapeSequence
HexEscapeSequence -> x HexDigit HexDigit
和模式语义:
The production CharacterEscape :: HexEscapeSequence
evaluates by evaluating the CV of the HexEscapeSequence
(see 7.8.4) and returning its character result.
在 DecimalEscape
: 的模式语义下,[=15=]
总是被解释为匹配 NUL 字符
The production DecimalEscape :: DecimalIntegerLiteral [lookahead ∉ DecimalDigit]
evaluates as follows:
- Let
i
be the MV of DecimalIntegerLiteral
.
- If
i
is zero, return the EscapeValue
consisting of a <NUL>
character (Unicode value 0000).
- Return the
EscapeValue
consisting of the integer i
.
The definition of "the MV of DecimalIntegerLiteral
" is in 7.8.3.
NOTE If \
is followed by a decimal number n
whose first digit is not 0, then the escape sequence is considered to be
a backreference. It is an error if n
is greater than the total number of left capturing parentheses in the entire regular
expression. [=15=]
represents the <NUL>
character and cannot be followed by a decimal digit.
因此,我不确定为什么 JSLint 禁止这些构造。从一些测试来看,它们似乎没有在解析器中实现,因为像这样的简单代码:
var x = /(['"])/;
抛出 "Unexpected '1' after '\'."
错误。
如果你想让代码通过JSLint,你可以用\u0000
指定NUL字符。否则,您可以忽略此错误。
我正在尝试验证我的整个脚本 through the new JSLint,但是:
function a() {
'use strict';
var string;
string = string.replace(/\x00*/g, '');
}
它抛出:
Unexpected 'x' after '\'.
string = string.replace(/\x00*/g, '');
旧版本不会引发任何错误。我知道它处于测试阶段,但我希望它能像旧版本一样好用。顺便说一句,\x00
代表空字符。
那么,ECMAScript 6 是否改变了什么?它只是一个 JSLint 错误吗?我真的做错了什么吗?
The "A regular expression literal can be confused with '/='" error is thrown when JSLint, JSHint (prior to version 1.0.0) or ESLint encounters a regular expression literal that begins with the = character. In the following example we attempt to assign a regular expression literal to match the string "=1" to the variable x:
This error is raised to highlight a potentially confusing piece of code. Your code will run fine if you do not fix this error, but it may be confusing to others, especially at first glance to someone quickly searching through your script.
The / character is ambiguous in JavaScript. It can either signify the start or end of a regular expression literal, as it does in the example above, or it can be interpreted as the division operator. Like most of the arithmetic operators, the division operator can be combined with the assignment operator to produce a shorthand:
https://jslinterrors.com/a-regular-expression-literal-can-be-confused-with
所以你需要使用RegExp
构造函数:
string.replace(new RegExp('\x00*', 'g'), '');
输出与正则表达式文字相同的正则表达式:
console.log(new RegExp('\x00*', 'g').toString() === /\x00*/g.toString()); // true
提示
NULL 字符 \x00
可以缩短为 [=15=]
(MDN docs)
new RegExp('\0*', 'g')
--
更新
@nhahtdh 的回答表明你可以使用 /\u0000*/g
literal.
根据 ECMAScript 规范:
\x00
有效,语法展开下:Atom -> \ AtomEscape AtomEscape -> CharacterEscape CharacterEscape -> HexEscapeSequence HexEscapeSequence -> x HexDigit HexDigit
和模式语义:
The production
CharacterEscape :: HexEscapeSequence
evaluates by evaluating the CV of theHexEscapeSequence
(see 7.8.4) and returning its character result.
在 [=15=]
总是被解释为匹配 NUL 字符The production
DecimalEscape :: DecimalIntegerLiteral [lookahead ∉ DecimalDigit]
evaluates as follows:- Let
i
be the MV ofDecimalIntegerLiteral
. - If
i
is zero, return theEscapeValue
consisting of a<NUL>
character (Unicode value 0000). - Return the
EscapeValue
consisting of the integeri
.
The definition of "the MV of
DecimalIntegerLiteral
" is in 7.8.3.NOTE If
\
is followed by a decimal numbern
whose first digit is not 0, then the escape sequence is considered to be a backreference. It is an error ifn
is greater than the total number of left capturing parentheses in the entire regular expression.[=15=]
represents the<NUL>
character and cannot be followed by a decimal digit.- Let
DecimalEscape
: 的模式语义下,因此,我不确定为什么 JSLint 禁止这些构造。从一些测试来看,它们似乎没有在解析器中实现,因为像这样的简单代码:
var x = /(['"])/;
抛出 "Unexpected '1' after '\'."
错误。
如果你想让代码通过JSLint,你可以用\u0000
指定NUL字符。否则,您可以忽略此错误。