如何修复此正则表达式以删除 JSLint 不必要的转义警告?

How to fix this regex to remove JSLint Unnecessary escape warning?

我得到一个 url,然后使用 React 中的 onClick 处理程序剥离域,使用这样的正则表达式:

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

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

因此,如果 url 是 http://example.com/my-super-funky-page,它会正确 console.log http://example.com 之后的所有内容 - 例如:

/my-super-funky-page

但我似乎对包含无用转义符的正则表达式有疑问。 JS Lint 报告:

Unnecessary escape character: \/  no-useless-escape

我需要删除什么才能使它按预期工作。我尝试了一些东西,但它破坏了结果。

/^.*\/\/[^/]+/

字符class中的/不需要转义

如果您不想收到不必要的转义通知,您可以安全地禁用此规则。

这将起作用: ^.*\/\/[^/]+

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.