简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM