问题
I have this line in my Javascript code:
var regex = /===Hello===\n/;
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?
回答1:
The problem is that /=
could be interpreted as a division and assignment, rather than the start of a regular expression literal.
You can avoid the warning by using the RegExp constructor instead:
var regex = new RegExp("===Hello===\n");
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.
来源:https://stackoverflow.com/questions/13286409/jshint-warning-a-regular-expression-literal-can-be-confused-with