JsHint警告:正则表达式文字可以与'/ ='混淆 [英] JsHint warning: A regular expression literal can be confused with '/='
问题描述
我在Javascript代码中有这一行:
I have this line in my Javascript code:
var regex = /===Hello===\n/;
JsHint在此行中给出了警告:
JsHint gives me a warning in this line:
A regular expression literal can be confused with '/='`
...但我不知道这个正则表达式有什么问题?如何避免此警告?
...but I don't know what's wrong with this regular expression? How can I avoid this warning?
推荐答案
问题是 / =
可以解释为除法和赋值,而不是正则表达式文字的开头。
The problem is that /=
could be interpreted as a division and assignment, rather than the start of a regular expression literal.
您可以使用 RegExp
构造函数:
You can avoid the warning by using the RegExp
constructor instead:
var regex = new RegExp("===Hello===\n");
似乎没有任何选项你可以设置告诉JSHint(或JSLint)忽略 / =
,所以你的选择要么解决它,要么忽略警告。
There doesn't appear to be any option you can set to tell JSHint (or JSLint for that matter) to ignore /=
, so your choice is either to work around it or ignore the warning.
这篇关于JsHint警告:正则表达式文字可以与'/ ='混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!