JsHint warning: A regular expression literal can be confused with '/='

随声附和 提交于 2019-12-02 11:44:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!