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

1.4k Views Asked by At

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/'.
E.g: 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?

1

There are 1 best solutions below

0
On

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.