简体   繁体   中英

Don't understand where my “Unnecessary escape character” error comes from

I have a url that contains two values, a keyword and a location name. The keyword comes after the '/r/' and the location after the '/l/'.
Eg: localhost:3000/search/r/keyword/l/location

To get only the value after the '/r/' (keyword) and the value after the '/l/' (location); I'm doing the following Regexr:

var cutResult = window.location.href.match(/\br\/\b(\b[^\/]+\b)/gi);
var cutLocation = window.location.href.match(/\b\/l\/\b(\b[^\/]+\b)/gi);

This does the trick, however, React returns following message:

Unnecessary escape character: / no-useless-escape

What causes the error?

You do not need to escape most of the characters inside a character class (inside [...]) only a few:

In most regex flavors, the only special characters or metacharacters inside a character class are the closing bracket ], the backslash \\, the caret ^, and the hyphen -. The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash. To search for a star or plus, use [+*]. Your regex will work fine if you escape the regular metacharacters inside a character class, but doing so significantly reduces readability.

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