admin管理员组

文章数量:1335363

I'm getting a url then stripping the domain with an onClick handler in React using a regEx like this :

const path = e.target.closest('a');
if (!path) return;
e.preventDefault();

console.log('path: ', path.href.replace(/^.*\/\/[^\/]+/, ''));

So if the url was say , it will correctly console.log everything after - eg :

/my-super-funky-page

But I seem to have an issue with the regEx containing a useless escape. JS Lint reports :

Unnecessary escape character: \/  no-useless-escape

What do I need to remove to still make this work as expected. I tried a few things and it broke the result.

I'm getting a url then stripping the domain with an onClick handler in React using a regEx like this :

const path = e.target.closest('a');
if (!path) return;
e.preventDefault();

console.log('path: ', path.href.replace(/^.*\/\/[^\/]+/, ''));

So if the url was say http://example./my-super-funky-page, it will correctly console.log everything after http://example. - eg :

/my-super-funky-page

But I seem to have an issue with the regEx containing a useless escape. JS Lint reports :

Unnecessary escape character: \/  no-useless-escape

What do I need to remove to still make this work as expected. I tried a few things and it broke the result.

Share Improve this question edited Nov 26, 2018 at 4:35 spice asked Nov 25, 2018 at 23:24 spicespice 1,5103 gold badges22 silver badges41 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6
/^.*\/\/[^/]+/

The / in the character class doesn't need escaping

If you don’t want to be notified about unnecessary escapes, you can safely disable this rule.

This will work: ^.*\/\/[^/]+

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.

Source

本文标签: javascriptHow to fix this regex to remove JSLint Unnecessary escape warningStack Overflow