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 badges2 Answers
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
版权声明:本文标题:javascript - How to fix this regex to remove JSLint Unnecessary escape warning? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742249906a2440556.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论